Skip to content

Commit

Permalink
ping: simplify ping loop
Browse files Browse the repository at this point in the history
  • Loading branch information
Stebalien committed May 7, 2019
1 parent d3d5351 commit d0ab451
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions p2p/protocol/ping/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,29 +99,24 @@ func Ping(ctx context.Context, h host.Host, p peer.ID) <-chan Result {
defer close(out)
defer cancel()

for {
for ctx.Err() == nil {
var res Result
res.RTT, res.Error = ping(s)

// canceled, ignore everything.
if ctx.Err() != nil {
return
}

// No error, record the RTT.
if res.Error == nil {
h.Peerstore().RecordLatency(p, res.RTT)
}

select {
case out <- res:
case <-ctx.Done():
return
default:
var res Result
res.RTT, res.Error = ping(s)

// canceled, ignore everything.
if ctx.Err() != nil {
return
}

// No error, record the RTT.
if res.Error == nil {
h.Peerstore().RecordLatency(p, res.RTT)
}

select {
case out <- res:
case <-ctx.Done():
return
}
}
}
}()
Expand Down

0 comments on commit d0ab451

Please sign in to comment.