Skip to content

Commit

Permalink
only connect if unconnected
Browse files Browse the repository at this point in the history
  • Loading branch information
gartnera committed Nov 23, 2024
1 parent 84beb1e commit 6c949b9
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions p2p/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,16 +212,20 @@ func (pd *PeerDiscovery) gossipPeers(ctx context.Context) {
defer wg.Done()
defer func() { <-sem }()

err := pd.host.Connect(ctx, p)
if err != nil {
pd.logger.Error().Err(err).
Stringer("to", p.ID).
Msg("Failed to connect to peer")
return
// only connect to peer if no active connections
// to prevent interruptions if address is changing
if len(pd.host.Network().ConnsToPeer(p.ID)) == 0 {
err := pd.host.Connect(ctx, p)
if err != nil {
pd.logger.Error().Err(err).
Stringer("to", p.ID).
Msg("Failed to connect to peer")
return
}
pd.logger.Debug().
Stringer("to", p).
Msg("Connected to peer")
}
pd.logger.Debug().
Stringer("to", p).
Msg("Connected to peer")

// Open discovery stream
s, err := pd.host.NewStream(ctx, p.ID, DiscoveryProtocol)
Expand Down

0 comments on commit 6c949b9

Please sign in to comment.