Skip to content

Commit 3b63466

Browse files
authored
fix(p2p): make bootstrapping non-blocking (#167)
We were waiting for all bootstrapper connections to either fail or succeed. The side effect of this approach is that a single connection may stall for various reasons, preventing the Exchange client from starting in an adequate amount of time. This patch ensures that every connection attempt has a generous timeout of 1m and that connections are not blocked for startup. We don't, in fact, need to wait for any connection to succeed and continue application startup. This also optimizes the startup time in the best case, as the application would be able to operate with new connections as they come and established, instead of waiting for the whole group of bootstrapper connections to either fall or succeed.
1 parent bdda26a commit 3b63466

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

p2p/peer_tracker.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,23 +72,18 @@ func newPeerTracker(
7272
}
7373
}
7474

75-
// bootstrap will bootstrap the peerTracker with the given trusted peers and if
75+
// bootstrap will initiate connections to the given trusted peers and if
7676
// a pidstore was given, will also attempt to bootstrap the tracker with previously
7777
// seen peers.
7878
//
7979
// NOTE: bootstrap is intended to be used with an on-disk peerstore.Peerstore as
8080
// the peerTracker needs access to the previously-seen peers' AddrInfo on start.
8181
func (p *peerTracker) bootstrap(ctx context.Context, trusted []libpeer.ID) error {
82-
// bootstrap connections to trusted
83-
wg := sync.WaitGroup{}
84-
wg.Add(len(trusted))
85-
defer wg.Wait()
82+
connectCtx, cancel := context.WithTimeout(context.Background(), time.Second*60)
83+
defer cancel()
84+
8685
for _, trust := range trusted {
87-
trust := trust
88-
go func() {
89-
defer wg.Done()
90-
p.connectToPeer(ctx, trust)
91-
}()
86+
go p.connectToPeer(connectCtx, trust)
9287
}
9388

9489
// short-circuit if pidstore was not provided
@@ -102,7 +97,7 @@ func (p *peerTracker) bootstrap(ctx context.Context, trusted []libpeer.ID) error
10297
}
10398

10499
for _, peer := range prevSeen {
105-
go p.connectToPeer(ctx, peer)
100+
go p.connectToPeer(connectCtx, peer)
106101
}
107102
return nil
108103
}

0 commit comments

Comments
 (0)