Skip to content

Commit

Permalink
filter connected peers right before connect attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
schomatis committed Apr 7, 2022
1 parent 47d9714 commit b4ad48f
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions core/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,14 +262,7 @@ func bootstrapRound(ctx context.Context, host host.Host, cfg BootstrapConfig) er
// but this list comes from restricted sets of original or temporary bootstrap
// nodes which will keep it under a sane value.)
func peersConnect(ctx context.Context, ph host.Host, availablePeers []peer.AddrInfo, needed int, permanent bool) uint64 {
// filter out nodes we are already connected to
var notConnected []peer.AddrInfo
for _, p := range availablePeers {
if ph.Network().Connectedness(p.ID) != network.Connected {
notConnected = append(notConnected, p)
}
}
peers := randomizeAddressList(notConnected)
peers := randomizeAddressList(availablePeers)

// Monitor the number of connections and stop if we reach the target.
var connected uint64
Expand Down Expand Up @@ -305,6 +298,12 @@ func peersConnect(ctx context.Context, ph host.Host, availablePeers []peer.AddrI
wg.Add(1)
go func(p peer.AddrInfo) {
defer wg.Done()

// Skip addresses belonging to a peer we're already connected to.
// (Not a guarantee but a best-effort policy.)
if ph.Network().Connectedness(p.ID) == network.Connected {
return
}
log.Debugf("%s bootstrapping to %s", ph.ID(), p.ID)

if err := ph.Connect(ctx, p); err != nil {
Expand Down

0 comments on commit b4ad48f

Please sign in to comment.