Skip to content

Commit 155714d

Browse files
committed
Improve ping API a bit by returning failure in case of only failure
License: MIT Signed-off-by: Jakub Sztandera <[email protected]>
1 parent 385055d commit 155714d

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

core/commands/ping.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ func pingPeer(ctx context.Context, n *core.IpfsNode, pid peer.ID, numPings int)
118118
if len(n.Peerstore.Addrs(pid)) == 0 {
119119
// Make sure we can find the node in question
120120
outChan <- &PingResult{
121-
Text: fmt.Sprintf("Looking up peer %s", pid.Pretty()),
121+
Text: fmt.Sprintf("Looking up peer %s", pid.Pretty()),
122+
Success: true,
122123
}
123124

124125
ctx, cancel := context.WithTimeout(ctx, kPingTimeout)
@@ -131,14 +132,18 @@ func pingPeer(ctx context.Context, n *core.IpfsNode, pid peer.ID, numPings int)
131132
n.Peerstore.AddAddrs(p.ID, p.Addrs, pstore.TempAddrTTL)
132133
}
133134

134-
outChan <- &PingResult{Text: fmt.Sprintf("PING %s.", pid.Pretty())}
135+
outChan <- &PingResult{
136+
Text: fmt.Sprintf("PING %s.", pid.Pretty()),
137+
Success: true,
138+
}
135139

136140
ctx, cancel := context.WithTimeout(ctx, kPingTimeout*time.Duration(numPings))
137141
defer cancel()
138142
pings, err := n.Ping.Ping(ctx, pid)
139143
if err != nil {
140144
log.Debugf("Ping error: %s", err)
141-
outChan <- &PingResult{Text: fmt.Sprintf("Ping error: %s", err)}
145+
outChan <- &PingResult{
146+
Text: fmt.Sprintf("Ping error: %s", err)}
142147
return
143148
}
144149

@@ -165,7 +170,8 @@ func pingPeer(ctx context.Context, n *core.IpfsNode, pid peer.ID, numPings int)
165170
}
166171
averagems := total.Seconds() * 1000 / float64(numPings)
167172
outChan <- &PingResult{
168-
Text: fmt.Sprintf("Average latency: %.2fms", averagems),
173+
Success: true,
174+
Text: fmt.Sprintf("Average latency: %.2fms", averagems),
169175
}
170176
}()
171177
return outChan

0 commit comments

Comments
 (0)