File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change 1+ package common
2+
3+ import (
4+ "context"
5+ "time"
6+
7+ "github.com/libp2p/go-libp2p/core/host"
8+ "github.com/libp2p/go-libp2p/core/peer"
9+ "github.com/libp2p/go-libp2p/p2p/protocol/ping"
10+ )
11+
12+ type Pinger interface {
13+ PingPeer (ctx context.Context , peerID peer.ID ) (time.Duration , error )
14+ }
15+
16+ type defaultPingImpl struct {
17+ host host.Host
18+ }
19+
20+ func NewDefaultPinger (host host.Host ) Pinger {
21+ return & defaultPingImpl {
22+ host : host ,
23+ }
24+ }
25+
26+ func (d * defaultPingImpl ) PingPeer (ctx context.Context , peerID peer.ID ) (time.Duration , error ) {
27+ pingResultCh := ping .Ping (ctx , d .host , peerID )
28+ select {
29+ case <- ctx .Done ():
30+ return 0 , ctx .Err ()
31+ case r := <- pingResultCh :
32+ if r .Error != nil {
33+ return 0 , r .Error
34+ }
35+ return r .RTT , nil
36+ }
37+ }
You can’t perform that action at this time.
0 commit comments