@cryptotaxi247 / kubo / commits / 1c9b6078e

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

License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>

Jakub Sztandera committed Jun 6, 2016 at 18:14 UTC 1c9b6078e3d49872830e6122c3b18feaee365c72
1 file changed +12 -4
core/commands/ping.go
+12 -4
@@ -118,7 +118,8 @@ func pingPeer(ctx context.Context, n *core.IpfsNode, pid peer.ID, numPings int)
118 if len(n.Peerstore.Addrs(pid)) == 0 {
119 // Make sure we can find the node in question
120 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,
123 }
124
125 ctx, cancel := context.WithTimeout(ctx, kPingTimeout)
@@ -131,14 +132,20 @@ func pingPeer(ctx context.Context, n *core.IpfsNode, pid peer.ID, numPings int)
132 n.Peerstore.AddAddrs(p.ID, p.Addrs, pstore.TempAddrTTL)
133 }
134
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 + }
139
140 ctx, cancel := context.WithTimeout(ctx, kPingTimeout*time.Duration(numPings))
141 defer cancel()
142 pings, err := n.Ping.Ping(ctx, pid)
143 if err != nil {
144 log.Debugf("Ping error: %s", err)
141 - outChan <- &PingResult{Text: fmt.Sprintf("Ping error: %s", err)}
145 + outChan <- &PingResult{
146 + Success: false,
147 + Text: fmt.Sprintf("Ping error: %s", err),
148 + }
149 return
150 }
151
@@ -165,7 +172,8 @@ func pingPeer(ctx context.Context, n *core.IpfsNode, pid peer.ID, numPings int)
172 }
173 averagems := total.Seconds() * 1000 / float64(numPings)
174 outChan <- &PingResult{
168 - Text: fmt.Sprintf("Average latency: %.2fms", averagems),
175 + Success: true,
176 + Text: fmt.Sprintf("Average latency: %.2fms", averagems),
177 }
178 }()
179 return outChan