-
Notifications
You must be signed in to change notification settings - Fork 199
/
libp2pConnectionMonitor.go
71 lines (55 loc) · 2.12 KB
/
libp2pConnectionMonitor.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
package libp2p
import (
"time"
"github.com/ElrondNetwork/elrond-go/p2p"
"github.com/libp2p/go-libp2p-core/network"
"github.com/multiformats/go-multiaddr"
)
// ThresholdMinimumConnectedPeers if the number of connected peers drop under this value, for each disconnecting
// peer, a trigger to reconnect to initial peers is done
var ThresholdMinimumConnectedPeers = 3
// DurationBetweenReconnectAttempts is used as to not call reconnecter.ReconnectToNetwork() to often
// when there are a lot of peers disconnecting and reconnection to initial nodes succeed
var DurationBetweenReconnectAttempts = time.Duration(time.Second * 5)
type libp2pConnectionMonitor struct {
chDoReconnect chan struct{}
reconnecter p2p.Reconnecter
}
func newLibp2pConnectionMonitor(reconnecter p2p.Reconnecter) *libp2pConnectionMonitor {
cm := &libp2pConnectionMonitor{
reconnecter: reconnecter,
chDoReconnect: make(chan struct{}, 0),
}
if reconnecter != nil {
go cm.doReconnection()
}
return cm
}
// Listen is called when network starts listening on an addr
func (lcm *libp2pConnectionMonitor) Listen(network.Network, multiaddr.Multiaddr) {}
// ListenClose is called when network stops listening on an addr
func (lcm *libp2pConnectionMonitor) ListenClose(network.Network, multiaddr.Multiaddr) {}
// Connected is called when a connection opened
func (lcm *libp2pConnectionMonitor) Connected(network.Network, network.Conn) {}
// Disconnected is called when a connection closed
func (lcm *libp2pConnectionMonitor) Disconnected(netw network.Network, conn network.Conn) {
if len(netw.Conns()) < ThresholdMinimumConnectedPeers {
select {
case lcm.chDoReconnect <- struct{}{}:
default:
}
}
}
// OpenedStream is called when a stream opened
func (lcm *libp2pConnectionMonitor) OpenedStream(network.Network, network.Stream) {}
// ClosedStream is called when a stream closed
func (lcm *libp2pConnectionMonitor) ClosedStream(network.Network, network.Stream) {}
func (lcm *libp2pConnectionMonitor) doReconnection() {
for {
select {
case <-lcm.chDoReconnect:
<-lcm.reconnecter.ReconnectToNetwork()
}
time.Sleep(DurationBetweenReconnectAttempts)
}
}