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

fix(p2p): make bootstrapping non-blocking #167

Merged
merged 1 commit into from
Mar 25, 2024
Merged
Changes from all commits
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
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()
Comment on lines -82 to -85
Copy link
Member

Choose a reason for hiding this comment

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

Does removing of waitgroup imply, that it doesn't need to wait for any connections to succeed anymore before returning from bootstrap()? Is it ok if all of the (or some) fail?

Copy link
Member Author

@Wondertan Wondertan Mar 25, 2024

Choose a reason for hiding this comment

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

Is it ok if all of the (or some) fail?

yes

Does removing of waitgroup imply, that it doesn't need to wait for any connections to succeed anymore before returning from bootstrap()?

Also yes, with a little caveat. The waiting for connections will still happen on the libp2p swarm level, if application decides to request anything and connections are still in progress

Copy link
Member

Choose a reason for hiding this comment

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

Just trying to understand what happens here. Looks like bootsrap() is acting as an optional call, since it does not wait for any connections and does not even has retries. So there might be 0 connections in the end. Does bootsrap() suppose to provide some connections to peer-tracker? If all of the connection fail would it result in some unwanted state? Would it make sense to add reconnects inside bootsrap in case 0 successful connections happened?

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
Loading