-
Notifications
You must be signed in to change notification settings - Fork 0
/
connections.go
128 lines (103 loc) · 2.65 KB
/
connections.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package gossip
import (
"container/list"
"errors"
"sync"
"github.com/mmvpm/gossip/service"
"google.golang.org/grpc"
)
type Connections struct {
mu sync.RWMutex
selfAddr string
lruSeedOrder *list.List
seedToData map[string]*service.PeerData
connections map[string]*grpc.ClientConn
}
func NewConnections(addr string) *Connections {
return &Connections{
mu: sync.RWMutex{},
selfAddr: addr,
lruSeedOrder: list.New(),
seedToData: map[string]*service.PeerData{addr: newPeerData(addr)},
connections: make(map[string]*grpc.ClientConn),
}
}
func (c *Connections) AddSeed(seed string, conn *grpc.ClientConn) {
c.mu.Lock()
defer c.mu.Unlock()
if _, exists := c.connections[seed]; !exists {
c.lruSeedOrder.PushFront(seed)
c.seedToData[seed] = newPeerData(seed)
c.connections[seed] = conn
}
}
func (c *Connections) DeleteSeed(seed string) {
c.mu.Lock()
defer c.mu.Unlock()
_ = c.connections[seed].Close()
delete(c.connections, seed)
c.seedToData[seed].Deleted = true
c.seedToData[seed].Version += 1
}
func (c *Connections) UpdateSelfInfo(info *PeerInfo) {
c.mu.Lock()
defer c.mu.Unlock()
c.seedToData[c.selfAddr] = updatePeerData(c.seedToData[c.selfAddr], info)
}
func (c *Connections) UpsertData(seed string, newData *service.PeerData) (alreadyExists bool) {
c.mu.Lock()
defer c.mu.Unlock()
curData, exists := c.seedToData[seed]
if !exists || newData.Version > curData.Version {
c.seedToData[seed] = newData
}
return exists
}
func (c *Connections) GetAllData() map[string]*service.PeerData {
c.mu.RLock()
defer c.mu.RUnlock()
members := make(map[string]*service.PeerData, len(c.connections)+1)
for addr, data := range c.seedToData {
if !data.Deleted {
members[addr] = data
}
}
return members
}
func (c *Connections) GetRandomConn() (string, *grpc.ClientConn, error) {
c.mu.RLock()
defer c.mu.RUnlock()
if len(c.connections) == 0 {
return "", nil, errors.New("error: no connections available")
}
for {
frontElem := c.lruSeedOrder.Front()
seed := frontElem.Value.(string)
if conn, exists := c.connections[seed]; exists {
c.lruSeedOrder.MoveToBack(frontElem)
return seed, conn, nil
} else {
c.lruSeedOrder.Remove(frontElem)
}
}
}
func (c *Connections) Close() {
c.mu.RLock()
defer c.mu.RUnlock()
for _, conn := range c.connections {
_ = conn.Close()
}
}
// internal
func newPeerData(seed string) *service.PeerData {
return &service.PeerData{Addr: seed}
}
func updatePeerData(data *service.PeerData, info *PeerInfo) *service.PeerData {
return &service.PeerData{
Addr: data.Addr,
Version: data.Version + 1,
Deleted: data.Deleted,
// PeerInfo
Name: info.Name,
}
}