mirror of
https://github.com/ipfs/kubo.git
synced 2025-07-03 04:37:30 +08:00
ping: use context
This commit is contained in:
@ -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
|
|||||||
|
|
||||||
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
|
|||||||
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
|
|||||||
|
|
||||||
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)
|
||||||
|
Reference in New Issue
Block a user