From 1c9b6078e3d49872830e6122c3b18feaee365c72 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Mon, 6 Jun 2016 18:14:29 +0200 Subject: [PATCH] Improve ping API a bit by returning failure in case of only failure License: MIT Signed-off-by: Jakub Sztandera --- core/commands/ping.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/core/commands/ping.go b/core/commands/ping.go index 156ec9968d5..5a15add7898 100644 --- a/core/commands/ping.go +++ b/core/commands/ping.go @@ -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