-
Notifications
You must be signed in to change notification settings - Fork 0
/
autorelay.go
63 lines (49 loc) · 1.34 KB
/
autorelay.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
package ezlibp2p
import (
"context"
"math/rand"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
)
func EnableAutoRelay(futureHost *host.Host) libp2p.Option {
peerSource := NewPeerstoreRelayPeerSource(futureHost)
return libp2p.EnableAutoRelayWithPeerSource(peerSource)
}
func NewPeerstoreRelayPeerSource(futureHost *host.Host) func(ctx context.Context, numPeers int) <-chan peer.AddrInfo {
var localID peer.ID = ""
var h host.Host = nil
return func(ctx context.Context, numPeers int) <-chan peer.AddrInfo {
if h == nil {
h = *futureHost
}
if localID == "" {
localID = h.ID()
}
source := make(chan peer.AddrInfo)
go func() {
peers := h.Peerstore().Peers()
// Add some randomness to the peer list.
rand.Shuffle(len(peers), func(i, j int) {
peers[i], peers[j] = peers[j], peers[i]
})
// From autorelay.PeerSource comment, we know that we need to return AT MOST numPeers.
// We might have less than numPeers.
maxPeersToSend := min(numPeers, len(peers))
for i := 0; i < maxPeersToSend; i++ {
select {
case <-ctx.Done():
break
default:
}
// Don't advertise self as a relay.
if peers[i] == localID {
continue
}
source <- h.Peerstore().PeerInfo(peers[i])
}
close(source)
}()
return source
}
}