Skip to content

Commit 7cf0c1e

Browse files
authored
update bootstrapPeers to be func() []peer.AddrInfo (#716)
* update bootstrapPeers to internally be func() []peer.AddrInfo * add BootstrapPeersFunc and add test
1 parent 6fff2a3 commit 7cf0c1e

File tree

5 files changed

+54
-9
lines changed

5 files changed

+54
-9
lines changed

dht.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,10 @@ type IpfsDHT struct {
126126

127127
autoRefresh bool
128128

129-
// A set of bootstrap peers to fallback on if all other attempts to fix
129+
// A function returning a set of bootstrap peers to fallback on if all other attempts to fix
130130
// the routing table fail (or, e.g., this is the first time this node is
131131
// connecting to the network).
132-
bootstrapPeers []peer.AddrInfo
132+
bootstrapPeers func() []peer.AddrInfo
133133

134134
maxRecordAge time.Duration
135135

@@ -473,15 +473,16 @@ func (dht *IpfsDHT) fixLowPeers(ctx context.Context) {
473473
// We should first use non-bootstrap peers we knew of from previous
474474
// snapshots of the Routing Table before we connect to the bootstrappers.
475475
// See https://github.com/libp2p/go-libp2p-kad-dht/issues/387.
476-
if dht.routingTable.Size() == 0 {
477-
if len(dht.bootstrapPeers) == 0 {
476+
if dht.routingTable.Size() == 0 && dht.bootstrapPeers != nil {
477+
bootstrapPeers := dht.bootstrapPeers()
478+
if len(bootstrapPeers) == 0 {
478479
// No point in continuing, we have no peers!
479480
return
480481
}
481482

482483
found := 0
483-
for _, i := range rand.Perm(len(dht.bootstrapPeers)) {
484-
ai := dht.bootstrapPeers[i]
484+
for _, i := range rand.Perm(len(bootstrapPeers)) {
485+
ai := bootstrapPeers[i]
485486
err := dht.Host().Connect(ctx, ai)
486487
if err == nil {
487488
found++

dht_options.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,18 @@ func RoutingTableFilter(filter RouteTableFilterFunc) Option {
269269
// and refresh our Routing Table if it becomes empty.
270270
func BootstrapPeers(bootstrappers ...peer.AddrInfo) Option {
271271
return func(c *dhtcfg.Config) error {
272-
c.BootstrapPeers = bootstrappers
272+
c.BootstrapPeers = func() []peer.AddrInfo {
273+
return bootstrappers
274+
}
275+
return nil
276+
}
277+
}
278+
279+
// BootstrapPeersFunc configures the function that returns the bootstrapping nodes that we will
280+
// connect to to seed and refresh our Routing Table if it becomes empty.
281+
func BootstrapPeersFunc(getBootstrapPeers func() []peer.AddrInfo) Option {
282+
return func(c *dhtcfg.Config) error {
283+
c.BootstrapPeers = getBootstrapPeers
273284
return nil
274285
}
275286
}

dht_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2051,6 +2051,39 @@ func TestBootStrapWhenRTIsEmpty(t *testing.T) {
20512051
}, 5*time.Second, 500*time.Millisecond)
20522052
}
20532053

2054+
func TestBootstrapPeersFunc(t *testing.T) {
2055+
ctx, cancel := context.WithCancel(context.Background())
2056+
defer cancel()
2057+
var lock sync.Mutex
2058+
2059+
bootstrapFuncA := func() []peer.AddrInfo {
2060+
return []peer.AddrInfo{}
2061+
}
2062+
dhtA := setupDHT(ctx, t, false, BootstrapPeersFunc(bootstrapFuncA))
2063+
2064+
bootstrapPeersB := []peer.AddrInfo{}
2065+
bootstrapFuncB := func() []peer.AddrInfo {
2066+
lock.Lock()
2067+
defer lock.Unlock()
2068+
return bootstrapPeersB
2069+
}
2070+
2071+
dhtB := setupDHT(ctx, t, false, BootstrapPeersFunc(bootstrapFuncB))
2072+
require.Equal(t, 0, len(dhtB.host.Network().Peers()))
2073+
2074+
addrA := peer.AddrInfo{
2075+
ID: dhtA.self,
2076+
Addrs: dhtA.host.Addrs(),
2077+
}
2078+
2079+
lock.Lock()
2080+
bootstrapPeersB = []peer.AddrInfo{addrA}
2081+
lock.Unlock()
2082+
2083+
dhtB.fixLowPeers(ctx)
2084+
require.NotEqual(t, 0, len(dhtB.host.Network().Peers()))
2085+
}
2086+
20542087
func TestPreconnectedNodes(t *testing.T) {
20552088
ctx, cancel := context.WithCancel(context.Background())
20562089
defer cancel()

fullrt/dht.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func NewFullRT(h host.Host, protocolPrefix protocol.ID, options ...Option) (*Ful
142142

143143
var bsPeers []*peer.AddrInfo
144144

145-
for _, ai := range dhtcfg.BootstrapPeers {
145+
for _, ai := range dhtcfg.BootstrapPeers() {
146146
tmpai := ai
147147
bsPeers = append(bsPeers, &tmpai)
148148
}

internal/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ type Config struct {
5757
DiversityFilter peerdiversity.PeerIPGroupFilter
5858
}
5959

60-
BootstrapPeers []peer.AddrInfo
60+
BootstrapPeers func() []peer.AddrInfo
6161

6262
// test specific Config options
6363
DisableFixLowPeers bool

0 commit comments

Comments
 (0)