forked from learning-at-home/go-libp2p-daemon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.go
96 lines (75 loc) · 1.92 KB
/
bootstrap.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
package p2pd
import (
"context"
"fmt"
"math/rand"
"time"
dht "github.com/libp2p/go-libp2p-kad-dht"
"github.com/chiangmaioneluv/go-libp2p/core/network"
"github.com/chiangmaioneluv/go-libp2p/core/peer"
"github.com/chiangmaioneluv/go-libp2p/core/peerstore"
)
var BootstrapPeers = dht.DefaultBootstrapPeers
const BootstrapConnections = 4
func bootstrapPeerInfo() ([]peer.AddrInfo, error) {
return peer.AddrInfosFromP2pAddrs(BootstrapPeers...)
}
func shufflePeerInfos(peers []peer.AddrInfo) {
for i := range peers {
j := rand.Intn(i + 1)
peers[i], peers[j] = peers[j], peers[i]
}
}
func (d *Daemon) Bootstrap() error {
pis, err := bootstrapPeerInfo()
if err != nil {
return err
}
for _, pi := range pis {
d.host.Peerstore().AddAddrs(pi.ID, pi.Addrs, peerstore.PermanentAddrTTL)
}
count := d.connectBootstrapPeers(pis, BootstrapConnections)
if count == 0 {
return fmt.Errorf("failed to connect to bootstrap peers")
}
go d.keepBootstrapConnections(pis)
if d.dht != nil {
return d.dht.Bootstrap(d.ctx)
}
return nil
}
func (d *Daemon) connectBootstrapPeers(pis []peer.AddrInfo, toconnect int) int {
count := 0
shufflePeerInfos(pis)
ctx, cancel := context.WithTimeout(d.ctx, 60*time.Second)
defer cancel()
for _, pi := range pis {
if d.host.Network().Connectedness(pi.ID) == network.Connected {
continue
}
err := d.host.Connect(ctx, pi)
if err != nil {
log.Debugw("Error connecting to bootstrap peer", "peer", pi.ID, "error", err)
} else {
d.host.ConnManager().TagPeer(pi.ID, "bootstrap", 1)
count++
toconnect--
}
if toconnect == 0 {
break
}
}
return count
}
func (d *Daemon) keepBootstrapConnections(pis []peer.AddrInfo) {
ticker := time.NewTicker(15 * time.Minute)
for {
<-ticker.C
conns := d.host.Network().Conns()
if len(conns) >= BootstrapConnections {
continue
}
toconnect := BootstrapConnections - len(conns)
d.connectBootstrapPeers(pis, toconnect)
}
}