forked from prysmaticlabs/prysm
-
Notifications
You must be signed in to change notification settings - Fork 2
/
watch_peers.go
55 lines (49 loc) · 1.31 KB
/
watch_peers.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
package p2p
import (
"context"
"time"
host "github.com/libp2p/go-libp2p-host"
)
// starPeerWatcher calls to reconnect any VIP peers such as the bootnode peer, the relay node peer
// or the static peers.
func startPeerWatcher(ctx context.Context, h host.Host, reconnectPeers ...string) {
go (func() {
for {
select {
case <-ctx.Done():
return
default:
ensurePeerConnections(ctx, h, reconnectPeers...)
// Wait 5 second to update again
time.Sleep(5 * time.Second)
}
}
})()
}
// ensurePeerConnections will attempt to reestablish connection to the peers
// if there are currently no connections to that peer.
func ensurePeerConnections(ctx context.Context, h host.Host, peers ...string) {
if len(peers) == 0 {
return
}
for _, p := range peers {
if p == "" {
continue
}
peer, err := MakePeer(p)
if err != nil {
log.Errorf("Could not make peer: %v", err)
continue
}
c := h.Network().ConnsToPeer(peer.ID)
if len(c) == 0 {
log.WithField("peer", peer.ID).Debug("No connections to peer, reconnecting")
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
if err := h.Connect(ctx, *peer); err != nil {
log.WithField("peer", peer.ID).WithField("addrs", peer.Addrs).WithError(err).Errorf("Failed to reconnect to peer")
continue
}
}
}
}