-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcluster.go
51 lines (42 loc) · 910 Bytes
/
cluster.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package proton
import "sync"
// Cluster represents a set of active
// raft members
type Cluster struct {
lock sync.RWMutex
peers map[uint64]*Peer
}
// Peer represents a raft cluster peer
type Peer struct {
*NodeInfo
Client *Raft
}
// NewCluster creates a new cluster neighbors
// list for a raft member
func NewCluster() *Cluster {
return &Cluster{
peers: make(map[uint64]*Peer),
}
}
// Peers returns the list of peers in the cluster
func (c *Cluster) Peers() map[uint64]*Peer {
peers := make(map[uint64]*Peer)
c.lock.RLock()
for k, v := range c.peers {
peers[k] = v
}
c.lock.RUnlock()
return peers
}
// AddPeer adds a node to our neighbors
func (c *Cluster) AddPeer(peer *Peer) {
c.lock.Lock()
c.peers[peer.ID] = peer
c.lock.Unlock()
}
// RemovePeer removes a node from our neighbors
func (c *Cluster) RemovePeer(id uint64) {
c.lock.Lock()
delete(c.peers, id)
c.lock.Unlock()
}