@@ -28,6 +28,7 @@ import (
28
28
rhost "github.com/libp2p/go-libp2p/p2p/host/routed"
29
29
"github.com/libp2p/go-libp2p/p2p/net/connmgr"
30
30
"github.com/libp2p/go-libp2p/p2p/net/upgrader"
31
+ "github.com/libp2p/go-libp2p/p2p/protocol/ping"
31
32
32
33
ma "github.com/multiformats/go-multiaddr"
33
34
)
@@ -68,6 +69,10 @@ const (
68
69
// to prevent uncontrolled message propagation.
69
70
const MaximumDisseminationTime = 90
70
71
72
+ // pingTimeout is the maximum duration of the ping test performed for
73
+ // freshly connected peers.
74
+ const pingTestTimeout = 60 * time .Second
75
+
71
76
// Config defines the configuration for the libp2p network provider.
72
77
type Config struct {
73
78
Bootstrap bool
@@ -320,7 +325,7 @@ func Connect(
320
325
return nil , err
321
326
}
322
327
323
- host .Network ().Notify (buildNotifiee ())
328
+ host .Network ().Notify (buildNotifiee (host ))
324
329
325
330
broadcastChannelManager , err := newChannelManager (ctx , identity , host , ticker )
326
331
if err != nil {
@@ -528,17 +533,20 @@ func extractMultiAddrFromPeers(peers []string) ([]peer.AddrInfo, error) {
528
533
return peerInfos , nil
529
534
}
530
535
531
- func buildNotifiee () libp2pnet.Notifiee {
536
+ func buildNotifiee (libp2pHost host. Host ) libp2pnet.Notifiee {
532
537
notifyBundle := & libp2pnet.NotifyBundle {}
533
538
534
539
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 ,
541
545
)
546
+
547
+ logger .Infof ("established connection to [%v]" , peerMultiaddress )
548
+
549
+ go executePingTest (libp2pHost , peerID , peerMultiaddress )
542
550
}
543
551
notifyBundle .DisconnectedF = func (_ libp2pnet.Network , connection libp2pnet.Conn ) {
544
552
logger .Infof (
@@ -553,6 +561,46 @@ func buildNotifiee() libp2pnet.Notifiee {
553
561
return notifyBundle
554
562
}
555
563
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
+
556
604
func multiaddressWithIdentity (
557
605
multiaddress ma.Multiaddr ,
558
606
peerID peer.ID ,
0 commit comments