Skip to content

Commit 2f10842

Browse files
fix(libp2p): increase ping timeout to 5s
1 parent f1a869a commit 2f10842

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

pkg/p2p/libp2p/libp2p.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -388,37 +388,34 @@ func New(ctx context.Context, signer beecrypto.Signer, networkID uint64, overlay
388388
wssResolver = r
389389
}
390390

391+
// Add WebSocket transport(s) based on configuration
391392
if o.EnableWSS {
392393
wsOpt := ws.WithTLSConfig(certManager.TLSConfig())
393394
transports = append(transports, libp2p.Transport(ws.New, wsOpt))
394-
}
395-
396-
if o.EnableWS {
395+
} else if o.EnableWS {
397396
transports = append(transports, libp2p.Transport(ws.New))
398397
}
399398

400399
opts = append(opts, libp2p.AddrsFactory(func(addrs []ma.Multiaddr) []ma.Multiaddr {
400+
// Always include NAT-resolved addresses (both cases use the same resolver logic
401401
addrs = includeNatResolvedAddresses(addrs, newCompositeAddressResolver(tcpResolver, wssResolver), logger)
402402

403+
// Only apply cert manager address rewriting when WSS is enabled
403404
if o.EnableWSS {
404-
// AddrsFactory takes the multiaddrs we're listening on and sets the multiaddrs to advertise to the network.
405-
// We use the AutoTLS address factory so that the `*` in the AutoTLS address string is replaced with the
406-
// actual IP address of the host once detected
407405
certManagerAddressFactory := certManager.AddressFactory()
408406
addrs = certManagerAddressFactory(addrs)
409407

408+
// Sort to prioritize public addresses (only meaningful with WSS, but harmless otherwise)
410409
slices.SortStableFunc(addrs, func(a, b ma.Multiaddr) int {
411410
aPub := manet.IsPublicAddr(a)
412411
bPub := manet.IsPublicAddr(b)
413-
switch {
414-
case aPub == bPub:
412+
if aPub == bPub {
415413
return 0
416-
case aPub:
414+
}
415+
if aPub {
417416
return -1
418-
case bPub:
419-
return 1
420417
}
421-
return 0
418+
return 1
422419
})
423420
}
424421

@@ -485,6 +482,7 @@ func New(ctx context.Context, signer beecrypto.Signer, networkID uint64, overlay
485482
return nil, fmt.Errorf("handshake service: %w", err)
486483
}
487484

485+
// TODO: check if it needs to have own isolated peerstore with dedicated resource manager?
488486
// Create a new dialer for libp2p ping protocol. This ensures that the protocol
489487
// uses a different set of keys to do ping. It prevents inconsistencies in peerstore as
490488
// the addresses used are not dialable and hence should be cleaned up. We should create
@@ -1082,9 +1080,10 @@ func (s *Service) Connect(ctx context.Context, addrs []ma.Multiaddr) (address *b
10821080
return nil, fmt.Errorf("connect full close %w", err)
10831081
}
10841082

1083+
// TODO: do we need to ping here? the handshake already verifies liveness?
10851084
var pingErr error
10861085
for _, addr := range addrs {
1087-
pingCtx, cancel := context.WithTimeout(ctx, 500*time.Millisecond)
1086+
pingCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
10881087
_, err := s.Ping(pingCtx, addr)
10891088
cancel() // Cancel immediately after use
10901089
if err == nil {

0 commit comments

Comments
 (0)