Skip to content

Commit 4052de0

Browse files
Execute a ping test for freshly connected peers
This test aims to diagnose potential problems with nodes that can initiate connections on their own but cannot be reached from the public internet. (cherry picked from commit b3d97ff)
1 parent 4e8061b commit 4052de0

1 file changed

Lines changed: 56 additions & 8 deletions

File tree

pkg/net/libp2p/libp2p.go

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
rhost "github.com/libp2p/go-libp2p/p2p/host/routed"
2929
"github.com/libp2p/go-libp2p/p2p/net/connmgr"
3030
"github.com/libp2p/go-libp2p/p2p/net/upgrader"
31+
"github.com/libp2p/go-libp2p/p2p/protocol/ping"
3132

3233
ma "github.com/multiformats/go-multiaddr"
3334
)
@@ -68,6 +69,10 @@ const (
6869
// to prevent uncontrolled message propagation.
6970
const MaximumDisseminationTime = 90
7071

72+
// pingTimeout is the maximum duration of the ping test performed for
73+
// freshly connected peers.
74+
const pingTestTimeout = 60 * time.Second
75+
7176
// Config defines the configuration for the libp2p network provider.
7277
type Config struct {
7378
Bootstrap bool
@@ -320,7 +325,7 @@ func Connect(
320325
return nil, err
321326
}
322327

323-
host.Network().Notify(buildNotifiee())
328+
host.Network().Notify(buildNotifiee(host))
324329

325330
broadcastChannelManager, err := newChannelManager(ctx, identity, host, ticker)
326331
if err != nil {
@@ -528,17 +533,20 @@ func extractMultiAddrFromPeers(peers []string) ([]peer.AddrInfo, error) {
528533
return peerInfos, nil
529534
}
530535

531-
func buildNotifiee() libp2pnet.Notifiee {
536+
func buildNotifiee(libp2pHost host.Host) libp2pnet.Notifiee {
532537
notifyBundle := &libp2pnet.NotifyBundle{}
533538

534539
notifyBundle.ConnectedF = func(_ libp2pnet.Network, connection libp2pnet.Conn) {
535-
logger.Infof(
536-
"established connection to [%v]",
537-
multiaddressWithIdentity(
538-
connection.RemoteMultiaddr(),
539-
connection.RemotePeer(),
540-
),
540+
peerID := connection.RemotePeer()
541+
542+
peerMultiaddress := multiaddressWithIdentity(
543+
connection.RemoteMultiaddr(),
544+
peerID,
541545
)
546+
547+
logger.Infof("established connection to [%v]", peerMultiaddress)
548+
549+
go executePingTest(libp2pHost, peerID, peerMultiaddress)
542550
}
543551
notifyBundle.DisconnectedF = func(_ libp2pnet.Network, connection libp2pnet.Conn) {
544552
logger.Infof(
@@ -553,6 +561,46 @@ func buildNotifiee() libp2pnet.Notifiee {
553561
return notifyBundle
554562
}
555563

564+
func executePingTest(
565+
libp2pHost host.Host,
566+
peerID peer.ID,
567+
peerMultiaddress string,
568+
) {
569+
logger.Infof("starting ping test for [%v]", peerMultiaddress)
570+
571+
ctx, cancelCtx := context.WithTimeout(
572+
context.Background(),
573+
pingTestTimeout,
574+
)
575+
defer cancelCtx()
576+
577+
resultChan := ping.Ping(ctx, libp2pHost, peerID)
578+
579+
select {
580+
case result := <-resultChan:
581+
if result.Error != nil {
582+
logger.Warnf(
583+
"ping test for [%v] failed: [%v]",
584+
peerMultiaddress,
585+
result.Error,
586+
)
587+
} else if result.Error == nil && result.RTT == 0 {
588+
logger.Warnf(
589+
"peer test for [%v] failed without clear reason",
590+
peerMultiaddress,
591+
)
592+
} else {
593+
logger.Infof(
594+
"ping test for [%v] completed with success (RTT [%v])",
595+
peerMultiaddress,
596+
result.RTT,
597+
)
598+
}
599+
case <-ctx.Done():
600+
logger.Warnf("ping test for [%v] timed out", peerMultiaddress)
601+
}
602+
}
603+
556604
func multiaddressWithIdentity(
557605
multiaddress ma.Multiaddr,
558606
peerID peer.ID,

0 commit comments

Comments
 (0)