forked from elastos/Elastos.ELA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifier.go
191 lines (159 loc) · 3.93 KB
/
notifier.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright (c) 2017-2019 The Elastos Foundation
// Use of this source code is governed by an MIT
// license that can be found in the LICENSE file.
//
package p2p
import (
"bytes"
"sort"
"strconv"
"strings"
"time"
"github.com/elastos/Elastos.ELA/dpos/p2p/peer"
)
// NotifyFlag identifies notifies should be callback.
type NotifyFlag int64
const (
// NFNetStabled is a flag to indicate network stabled.
NFNetStabled NotifyFlag = 1 << iota
// NFBadNetwork is a flag to indicate network unstable.
NFBadNetwork
)
var nfStrings = map[NotifyFlag]string{
NFBadNetwork: "NFBadNetwork",
}
var orderedNfStrings = []NotifyFlag{
NFBadNetwork,
}
// String returns the NotifyFlag in human-readable form.
func (f NotifyFlag) String() string {
// No flags are set.
if f == 0 {
return "0x0"
}
// Add individual bit flags.
s := ""
for _, flag := range orderedNfStrings {
if f&flag == flag {
s += nfStrings[flag] + "|"
f -= flag
}
}
// Add any remaining flags which aren't accounted for as hex.
s = strings.TrimRight(s, "|")
if f != 0 {
s += "|0x" + strconv.FormatUint(uint64(f), 16)
}
s = strings.TrimLeft(s, "|")
return s
}
// Ensure Notifier implement StateNotifier interface.
var _ StateNotifier = (*Notifier)(nil)
const (
// ConnectionTimeout is the duration we timeout peer connections.
ConnectionTimeout = time.Second * 30
)
type Notifier struct {
flags NotifyFlag
notify func(flag NotifyFlag)
peers chan []peer.PID
newPeer chan peer.PID
donePeer chan peer.PID
}
func (n *Notifier) OnConnectPeers(peers []peer.PID) {
n.peers <- peers
}
func (n *Notifier) OnNewPeer(pid peer.PID) {
n.newPeer <- pid
}
func (n *Notifier) OnDonePeer(pid peer.PID) {
n.donePeer <- pid
}
func (n *Notifier) notifyHandler() {
// The current connect peer addresses.
var peers []peer.PID
// Connected peers whether inbound or outbound or both.
var connected = make(map[peer.PID]int)
// The timeout timer used to trigger peers connection timeout.
var timer = time.NewTimer(ConnectionTimeout)
timer.Stop()
// stable mark if server have connected to enough peers.
var stable bool
startTimer := func() {
stable = false
timer.Reset(ConnectionTimeout)
}
for {
select {
case list := <-n.peers:
// New ConnectPeers message received.
if !equalList(peers, list) {
peers = list
startTimer()
}
case pid := <-n.newPeer:
// There will be two peers(inbound and outbound) with the same pid
// connected.
connected[pid]++
// Connected peers reach 2/3, mark server state as stable.
if !stable && len(connected)/2 >= len(peers)/3 {
timer.Stop()
stable = true
if n.flags&NFNetStabled == NFNetStabled {
go n.notify(NFNetStabled)
}
}
case pid := <-n.donePeer:
// When both inbound and outbound with the same pid disconnected,
// remove the pid key.
connected[pid]--
if connected[pid] == 0 {
delete(connected, pid)
}
// Stabled server turn to unstable.
if stable && len(connected)/2 < len(peers)/3 {
if n.flags&NFBadNetwork == NFBadNetwork {
go n.notify(NFBadNetwork)
}
startTimer()
}
case <-timer.C:
if len(connected)/2 < len(peers)/3 {
if n.flags&NFBadNetwork == NFBadNetwork {
go n.notify(NFBadNetwork)
}
startTimer()
}
}
}
}
func NewNotifier(flags NotifyFlag, notify func(flag NotifyFlag)) *Notifier {
n := Notifier{
flags: flags,
notify: notify,
peers: make(chan []peer.PID),
newPeer: make(chan peer.PID),
donePeer: make(chan peer.PID),
}
go n.notifyHandler()
return &n
}
func equalList(list1, list2 []peer.PID) bool {
if len(list1) != len(list2) || list1 == nil || list2 == nil {
return false
}
list1, list2 = sortPeers(list1), sortPeers(list2)
for i := range list1 {
if list1[i].Equal(list2[i]) {
continue
}
return false
}
return true
}
func sortPeers(peers []peer.PID) []peer.PID {
sort.Slice(peers, func(i, j int) bool {
return bytes.Compare(peers[i][:], peers[j][:]) < 0
})
return peers
}