diff --git a/pkg/p2p/libp2p/libp2p.go b/pkg/p2p/libp2p/libp2p.go index 6bab9b8214c..cfc2f83d15f 100644 --- a/pkg/p2p/libp2p/libp2p.go +++ b/pkg/p2p/libp2p/libp2p.go @@ -997,6 +997,7 @@ 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. + for _, addr := range addrs { // Extract the peer ID from the multiaddr. ai, err := libp2ppeer.AddrInfoFromP2pAddr(addr) @@ -1029,7 +1030,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(ctx, 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()) diff --git a/pkg/topology/kademlia/kademlia.go b/pkg/topology/kademlia/kademlia.go index b31dd6d6c6d..d4b28e8eb1b 100644 --- a/pkg/topology/kademlia/kademlia.go +++ b/pkg/topology/kademlia/kademlia.go @@ -42,10 +42,9 @@ const ( addPeerBatchSize = 500 - // To avoid context.Timeout errors during network failure, the value of - // the peerConnectionAttemptTimeout constant must be equal to or greater - // than 5 seconds (empirically verified). - peerConnectionAttemptTimeout = 15 * time.Second // timeout for establishing a new connection with peer. + // Each underlay address gets up to 15s for connection (in libp2p.Connect). + // This budget allows multiple addresses to be tried sequentially per peer. + peerConnectionAttemptTimeout = 45 * time.Second // timeout for establishing a new connection with peer. ) // Default option values @@ -807,9 +806,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 @@ -820,6 +816,10 @@ func (k *Kad) connectBootNodes(ctx context.Context) { if attempts >= maxBootNodeAttempts { return true, nil } + + ctx, cancel := context.WithTimeout(ctx, peerConnectionAttemptTimeout) + defer cancel() + bzzAddress, err := k.p2p.Connect(ctx, []ma.Multiaddr{addr}) attempts++