CallNodeWithContext calls node method with context.
(
ctx context.Context, node proto.NodeID, method string, args, reply interface{})
| 82 | |
| 83 | // CallNodeWithContext calls node method with context. |
| 84 | func (c *Caller) CallNodeWithContext( |
| 85 | ctx context.Context, node proto.NodeID, method string, args, reply interface{}) (err error, |
| 86 | ) { |
| 87 | startTime := time.Now() |
| 88 | defer func() { |
| 89 | recordRPCCost(startTime, method, err) |
| 90 | }() |
| 91 | |
| 92 | client, err := DialToNodeWithPool(c.pool, node, method == route.DHTPing.String()) |
| 93 | if err != nil { |
| 94 | err = errors.Wrapf(err, "dial to node %s failed", node) |
| 95 | return |
| 96 | } |
| 97 | defer func() { _ = client.Close() }() |
| 98 | |
| 99 | // TODO(xq262144): golang net/rpc does not support cancel in progress calls |
| 100 | ch := client.Go(method, args, reply, make(chan *rpc.Call, 1)) |
| 101 | |
| 102 | select { |
| 103 | case <-ctx.Done(): |
| 104 | err = ctx.Err() |
| 105 | case call := <-ch.Done: |
| 106 | err = call.Error |
| 107 | // Set error state so that the associated will not reuse this client |
| 108 | if err != nil { // TODO(leventeliu): check recoverable errors |
| 109 | if setter, ok := client.(LastErrSetter); ok { |
| 110 | setter.SetLastErr(err) |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | return |
| 116 | } |
| 117 | |
| 118 | // CallNode calls node method. |
| 119 | func (c *Caller) CallNode(node proto.NodeID, method string, args, reply interface{}) (err error) { |