Skip to content
Open
Show file tree
Hide file tree
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: 16 additions & 1 deletion pkg/p2p/libp2p/libp2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,18 @@ func (s *Service) Connect(ctx context.Context, addrs []ma.Multiaddr) (address *b
// addresses for the same peer is passed to the host.Host.Connect function
// and reachabiltiy Private is emitted on libp2p EventBus(), which results
// in weaker connectivity and failures in some integration tests.

baseCtx := context.WithoutCancel(ctx)

for _, addr := range addrs {
// Check if parent context is cancelled (shutdown, etc.)
if ctx.Err() != nil {
if connectErr == nil {
connectErr = ctx.Err()
}
break
}

// Extract the peer ID from the multiaddr.
ai, err := libp2ppeer.AddrInfoFromP2pAddr(addr)
if err != nil {
Expand Down Expand Up @@ -1029,7 +1040,11 @@ func (s *Service) Connect(ctx context.Context, addrs []ma.Multiaddr) (address *b
return address, p2p.ErrAlreadyConnected
}

if err := s.connectionBreaker.Execute(func() error { return s.host.Connect(ctx, *info) }); err != nil {
connectCtx, cancel := context.WithTimeout(baseCtx, 15*time.Second)
err = s.connectionBreaker.Execute(func() error { return s.host.Connect(connectCtx, *info) })
cancel()

if err != nil {
if errors.Is(err, breaker.ErrClosed) {
s.metrics.ConnectBreakerCount.Inc()
return nil, p2p.NewConnectionBackoffError(err, s.connectionBreaker.ClosedUntil())
Expand Down
6 changes: 0 additions & 6 deletions pkg/topology/kademlia/kademlia.go
Original file line number Diff line number Diff line change
Expand Up @@ -807,9 +807,6 @@ func (k *Kad) connectBootNodes(ctx context.Context) {
var attempts, connected int
totalAttempts := maxBootNodeAttempts * len(k.opt.Bootnodes)

ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
defer cancel()

for _, addr := range k.opt.Bootnodes {
if attempts >= totalAttempts || connected >= 3 {
return
Expand Down Expand Up @@ -960,9 +957,6 @@ func (k *Kad) recalcDepth() {
func (k *Kad) connect(ctx context.Context, peer swarm.Address, ma []ma.Multiaddr) error {
k.logger.Debug("attempting connect to peer", "peer_address", peer)

ctx, cancel := context.WithTimeout(ctx, peerConnectionAttemptTimeout)
defer cancel()

k.metrics.TotalOutboundConnectionAttempts.Inc()

switch i, err := k.p2p.Connect(ctx, ma); {
Expand Down
Loading