Skip to content

Commit

Permalink
fix(p2p): make bootstrapping non-blocking (#167)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Wondertan authored Mar 25, 2024
1 parent bdda26a commit 3b63466
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions p2p/peer_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,18 @@ func newPeerTracker(
}
}

// bootstrap will bootstrap the peerTracker with the given trusted peers and if
// bootstrap will initiate connections to the given trusted peers and if
// a pidstore was given, will also attempt to bootstrap the tracker with previously
// seen peers.
//
// NOTE: bootstrap is intended to be used with an on-disk peerstore.Peerstore as
// the peerTracker needs access to the previously-seen peers' AddrInfo on start.
func (p *peerTracker) bootstrap(ctx context.Context, trusted []libpeer.ID) error {
// bootstrap connections to trusted
wg := sync.WaitGroup{}
wg.Add(len(trusted))
defer wg.Wait()
connectCtx, cancel := context.WithTimeout(context.Background(), time.Second*60)
defer cancel()

for _, trust := range trusted {
trust := trust
go func() {
defer wg.Done()
p.connectToPeer(ctx, trust)
}()
go p.connectToPeer(connectCtx, trust)
}

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

for _, peer := range prevSeen {
go p.connectToPeer(ctx, peer)
go p.connectToPeer(connectCtx, peer)
}
return nil
}
Expand Down

0 comments on commit 3b63466

Please sign in to comment.