mirror of
https://github.com/ipfs/kubo.git
synced 2026-03-01 14:28:02 +08:00
Merge pull request #2813 from ipfs/feature/ping-api
Improve ping API a bit by returning failure in case of only failure
This commit is contained in:
commit
ecbca7e0f4
@ -118,7 +118,8 @@ func pingPeer(ctx context.Context, n *core.IpfsNode, pid peer.ID, numPings int)
|
||||
if len(n.Peerstore.Addrs(pid)) == 0 {
|
||||
// Make sure we can find the node in question
|
||||
outChan <- &PingResult{
|
||||
Text: fmt.Sprintf("Looking up peer %s", pid.Pretty()),
|
||||
Text: fmt.Sprintf("Looking up peer %s", pid.Pretty()),
|
||||
Success: true,
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(ctx, kPingTimeout)
|
||||
@ -131,14 +132,20 @@ func pingPeer(ctx context.Context, n *core.IpfsNode, pid peer.ID, numPings int)
|
||||
n.Peerstore.AddAddrs(p.ID, p.Addrs, pstore.TempAddrTTL)
|
||||
}
|
||||
|
||||
outChan <- &PingResult{Text: fmt.Sprintf("PING %s.", pid.Pretty())}
|
||||
outChan <- &PingResult{
|
||||
Text: fmt.Sprintf("PING %s.", pid.Pretty()),
|
||||
Success: true,
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(ctx, kPingTimeout*time.Duration(numPings))
|
||||
defer cancel()
|
||||
pings, err := n.Ping.Ping(ctx, pid)
|
||||
if err != nil {
|
||||
log.Debugf("Ping error: %s", err)
|
||||
outChan <- &PingResult{Text: fmt.Sprintf("Ping error: %s", err)}
|
||||
outChan <- &PingResult{
|
||||
Success: false,
|
||||
Text: fmt.Sprintf("Ping error: %s", err),
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@ -165,7 +172,8 @@ func pingPeer(ctx context.Context, n *core.IpfsNode, pid peer.ID, numPings int)
|
||||
}
|
||||
averagems := total.Seconds() * 1000 / float64(numPings)
|
||||
outChan <- &PingResult{
|
||||
Text: fmt.Sprintf("Average latency: %.2fms", averagems),
|
||||
Success: true,
|
||||
Text: fmt.Sprintf("Average latency: %.2fms", averagems),
|
||||
}
|
||||
}()
|
||||
return outChan
|
||||
|
||||
Loading…
Reference in New Issue
Block a user