Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update bootstrapPeers to be func() []peer.AddrInfo #716

Merged
merged 6 commits into from
Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ type IpfsDHT struct {

autoRefresh bool

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

maxRecordAge time.Duration

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

found := 0
for _, i := range rand.Perm(len(dht.bootstrapPeers)) {
ai := dht.bootstrapPeers[i]
for _, i := range rand.Perm(len(bootstrapPeers)) {
ai := bootstrapPeers[i]
err := dht.Host().Connect(ctx, ai)
if err == nil {
found++
Expand Down
4 changes: 2 additions & 2 deletions dht_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,9 @@ func RoutingTableFilter(filter RouteTableFilterFunc) Option {

// BootstrapPeers configures the bootstrapping nodes that we will connect to to seed
// and refresh our Routing Table if it becomes empty.
func BootstrapPeers(bootstrappers ...peer.AddrInfo) Option {
func BootstrapPeers(getBootstrapPeers func() []peer.AddrInfo) Option {
return func(c *dhtcfg.Config) error {
c.BootstrapPeers = bootstrappers
c.BootstrapPeers = getBootstrapPeers
return nil
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of modifying this option's function signature and breaking existing users could you add a new one instead? This one could then just be a function that returns the passed in addrinfos.

Expand Down
8 changes: 6 additions & 2 deletions dht_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1991,7 +1991,9 @@ func TestBootStrapWhenRTIsEmpty(t *testing.T) {
testPrefix,
NamespacedValidator("v", blankValidator{}),
Mode(ModeServer),
BootstrapPeers(bootstrapAddrs[0]),
BootstrapPeers(func() []peer.AddrInfo {
return []peer.AddrInfo{bootstrapAddrs[0]}
}),
)
require.NoError(t, err)
dht2 := setupDHT(ctx, t, false)
Expand Down Expand Up @@ -2025,7 +2027,9 @@ func TestBootStrapWhenRTIsEmpty(t *testing.T) {
testPrefix,
NamespacedValidator("v", blankValidator{}),
Mode(ModeServer),
BootstrapPeers(bootstrapAddrs[1], bootstrapAddrs[2]),
BootstrapPeers(func() []peer.AddrInfo {
return []peer.AddrInfo{bootstrapAddrs[1], bootstrapAddrs[2]}
}),
)
require.NoError(t, err)

Expand Down
2 changes: 1 addition & 1 deletion fullrt/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func NewFullRT(h host.Host, protocolPrefix protocol.ID, options ...Option) (*Ful

var bsPeers []*peer.AddrInfo

for _, ai := range dhtcfg.BootstrapPeers {
for _, ai := range dhtcfg.BootstrapPeers() {
tmpai := ai
bsPeers = append(bsPeers, &tmpai)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type Config struct {
DiversityFilter peerdiversity.PeerIPGroupFilter
}

BootstrapPeers []peer.AddrInfo
BootstrapPeers func() []peer.AddrInfo

// test specific Config options
DisableFixLowPeers bool
Expand Down