-
Notifications
You must be signed in to change notification settings - Fork 28
/
connectedness_manager.go
152 lines (123 loc) · 3.36 KB
/
connectedness_manager.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package weshnet
import (
"context"
"sync"
peer "github.com/libp2p/go-libp2p/core/peer"
"berty.tech/weshnet/v2/internal/notify"
)
type ConnectednessType int
const (
ConnectednessTypeDisconnected ConnectednessType = iota
ConnectednessTypeReconnecting
ConnectednessTypeConnected
)
type ConnectednessUpdate struct {
Peer peer.ID
Status ConnectednessType
}
type PeersConnectedness map[peer.ID]ConnectednessType
type GroupStatus struct {
peers map[peer.ID]*PeerStatus
notify *notify.Notify
}
type PeerStatus struct {
groups map[string]*GroupStatus
status ConnectednessType
}
type ConnectednessManager struct {
peerState map[peer.ID]*PeerStatus
groupState map[string]*GroupStatus
muState sync.Mutex
}
func NewConnectednessManager() *ConnectednessManager {
return &ConnectednessManager{
peerState: make(map[peer.ID]*PeerStatus),
groupState: make(map[string]*GroupStatus),
}
}
// AssociatePeer associate a peer to a group
func (m *ConnectednessManager) AssociatePeer(group string, peer peer.ID) {
m.muState.Lock()
defer m.muState.Unlock()
sg := m.getGroupStatus(group)
sp := m.getPeerStatus(peer)
sg.notify.L.Lock()
if _, ok := sg.peers[peer]; !ok {
// we got a new peer, update and signal an update
sg.peers[peer] = sp
sp.groups[group] = sg
sg.notify.Broadcast()
}
sg.notify.L.Unlock()
}
// UpdateState update peer current connectedness state
func (m *ConnectednessManager) UpdateState(peer peer.ID, update ConnectednessType) {
m.muState.Lock()
defer m.muState.Unlock()
sp := m.getPeerStatus(peer)
if sp.status != update {
sp.status = update
// notify each group that need an update
for _, g := range sp.groups {
g.notify.Broadcast()
}
}
}
// WaitForConnectednessChange wait until the given `current` peers status differ from `local` peers state
func (m *ConnectednessManager) WaitForConnectednessChange(ctx context.Context, gkey string, current PeersConnectedness) ([]peer.ID, bool) {
m.muState.Lock()
sg := m.getGroupStatus(gkey)
m.muState.Unlock()
ok := true
sg.notify.L.Lock()
var updated []peer.ID
for ok {
// check if there are some diff between local state and the current state
if updated = m.updateStatus(sg, current); len(updated) > 0 {
break // we got some update, leave the loop
}
// wait until there is an update on this group or context expire
// unlock notify locker
ok = sg.notify.Wait(ctx)
}
sg.notify.L.Unlock()
return updated, ok
}
func (m *ConnectednessManager) getGroupStatus(gkey string) *GroupStatus {
s, ok := m.groupState[gkey]
if !ok {
s = &GroupStatus{
peers: make(map[peer.ID]*PeerStatus),
notify: notify.New(&sync.Mutex{}),
}
m.groupState[gkey] = s
}
return s
}
func (m *ConnectednessManager) getPeerStatus(peer peer.ID) *PeerStatus {
s, ok := m.peerState[peer]
if !ok {
s = &PeerStatus{
groups: make(map[string]*GroupStatus),
}
m.peerState[peer] = s
}
return s
}
func (m *ConnectednessManager) updateStatus(group *GroupStatus, current PeersConnectedness) []peer.ID {
m.muState.Lock()
updated := []peer.ID{}
for peer := range group.peers {
if ourPeer, ok := m.peerState[peer]; ok {
theirStatus, ok := current[peer]
if ok && ourPeer.status == theirStatus {
continue // we share the same state for that peer, skip
}
// update peer status
current[peer] = ourPeer.status
updated = append(updated, peer)
}
}
m.muState.Unlock()
return updated
}