Skip to content
Merged
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
7 changes: 6 additions & 1 deletion pkg/p2p/libp2p/libp2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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())
Expand Down
14 changes: 7 additions & 7 deletions pkg/topology/kademlia/kademlia.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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++
Expand Down
Loading