Skip to content

Commit

Permalink
Improve ping API a bit by returning failure in case of only failure
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
  • Loading branch information
Kubuxu committed Jun 6, 2016
1 parent 385055d commit 155714d
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions core/commands/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -131,14 +132,18 @@ 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{
Text: fmt.Sprintf("Ping error: %s", err)}
return
}

Expand All @@ -165,7 +170,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
Expand Down

0 comments on commit 155714d

Please sign in to comment.