提交 1c9b6078 作者: Jakub Sztandera

Improve ping API a bit by returning failure in case of only failure

License: MIT
Signed-off-by: 's avatarJakub Sztandera <kubuxu@protonmail.ch>
上级 385055d2
...@@ -119,6 +119,7 @@ func pingPeer(ctx context.Context, n *core.IpfsNode, pid peer.ID, numPings int) ...@@ -119,6 +119,7 @@ func pingPeer(ctx context.Context, n *core.IpfsNode, pid peer.ID, numPings int)
// Make sure we can find the node in question // Make sure we can find the node in question
outChan <- &PingResult{ 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) ctx, cancel := context.WithTimeout(ctx, kPingTimeout)
...@@ -131,14 +132,20 @@ func pingPeer(ctx context.Context, n *core.IpfsNode, pid peer.ID, numPings int) ...@@ -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) 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)) ctx, cancel := context.WithTimeout(ctx, kPingTimeout*time.Duration(numPings))
defer cancel() defer cancel()
pings, err := n.Ping.Ping(ctx, pid) pings, err := n.Ping.Ping(ctx, pid)
if err != nil { if err != nil {
log.Debugf("Ping error: %s", err) 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 return
} }
...@@ -165,6 +172,7 @@ func pingPeer(ctx context.Context, n *core.IpfsNode, pid peer.ID, numPings int) ...@@ -165,6 +172,7 @@ func pingPeer(ctx context.Context, n *core.IpfsNode, pid peer.ID, numPings int)
} }
averagems := total.Seconds() * 1000 / float64(numPings) averagems := total.Seconds() * 1000 / float64(numPings)
outChan <- &PingResult{ outChan <- &PingResult{
Success: true,
Text: fmt.Sprintf("Average latency: %.2fms", averagems), Text: fmt.Sprintf("Average latency: %.2fms", averagems),
} }
}() }()
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论