Commit f7941e98 authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

ping: use context

parent 374a75b6
...@@ -72,6 +72,7 @@ Send pings to a peer using the routing system to discover its address ...@@ -72,6 +72,7 @@ Send pings to a peer using the routing system to discover its address
}, },
}, },
Run: func(req cmds.Request) (interface{}, error) { Run: func(req cmds.Request) (interface{}, error) {
ctx := req.Context().Context
n, err := req.Context().GetNode() n, err := req.Context().GetNode()
if err != nil { if err != nil {
return nil, err return nil, err
...@@ -103,14 +104,14 @@ Send pings to a peer using the routing system to discover its address ...@@ -103,14 +104,14 @@ Send pings to a peer using the routing system to discover its address
outChan := make(chan interface{}) outChan := make(chan interface{})
go pingPeer(n, peerID, numPings, outChan) go pingPeer(ctx, n, peerID, numPings, outChan)
return outChan, nil return outChan, nil
}, },
Type: PingResult{}, Type: PingResult{},
} }
func pingPeer(n *core.IpfsNode, pid peer.ID, numPings int, outChan chan interface{}) { func pingPeer(ctx context.Context, n *core.IpfsNode, pid peer.ID, numPings int, outChan chan interface{}) {
defer close(outChan) defer close(outChan)
if len(n.Peerstore.Addresses(pid)) == 0 { if len(n.Peerstore.Addresses(pid)) == 0 {
...@@ -119,8 +120,7 @@ func pingPeer(n *core.IpfsNode, pid peer.ID, numPings int, outChan chan interfac ...@@ -119,8 +120,7 @@ func pingPeer(n *core.IpfsNode, pid peer.ID, numPings int, outChan chan interfac
Text: fmt.Sprintf("Looking up peer %s", pid.Pretty()), Text: fmt.Sprintf("Looking up peer %s", pid.Pretty()),
} }
// TODO: get master context passed in ctx, _ := context.WithTimeout(ctx, kPingTimeout)
ctx, _ := context.WithTimeout(context.TODO(), kPingTimeout)
p, err := n.Routing.FindPeer(ctx, pid) p, err := n.Routing.FindPeer(ctx, pid)
if err != nil { if err != nil {
outChan <- &PingResult{Text: fmt.Sprintf("Peer lookup error: %s", err)} outChan <- &PingResult{Text: fmt.Sprintf("Peer lookup error: %s", err)}
...@@ -131,9 +131,17 @@ func pingPeer(n *core.IpfsNode, pid peer.ID, numPings int, outChan chan interfac ...@@ -131,9 +131,17 @@ func pingPeer(n *core.IpfsNode, pid peer.ID, numPings int, outChan chan interfac
outChan <- &PingResult{Text: fmt.Sprintf("PING %s.", pid.Pretty())} outChan <- &PingResult{Text: fmt.Sprintf("PING %s.", pid.Pretty())}
var done bool
var total time.Duration var total time.Duration
for i := 0; i < numPings; i++ { for i := 0; i < numPings && !done; i++ {
ctx, _ := context.WithTimeout(context.TODO(), kPingTimeout) select {
case <-ctx.Done():
done = true
continue
default:
}
ctx, _ := context.WithTimeout(ctx, kPingTimeout)
took, err := n.Routing.Ping(ctx, pid) took, err := n.Routing.Ping(ctx, pid)
if err != nil { if err != nil {
log.Errorf("Ping error: %s", err) log.Errorf("Ping error: %s", err)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment