Call 向指定 node 发起一次请求-响应调用。 reqBody 会用 encoding/json 编码(nil 表示空 body)。 返回值是 node 的 NodeMessage.Body(json 原文)。 当 ctx 取消时,hub 会 best-effort 向 node 发送 cancel_id 帧, 然后返回 ctx.Err()。
(ctx context.Context, nodeID, method string, reqBody any)
| 22 | // 当 ctx 取消时,hub 会 best-effort 向 node 发送 cancel_id 帧, |
| 23 | // 然后返回 ctx.Err()。 |
| 24 | func (h *Hub) Call(ctx context.Context, nodeID, method string, reqBody any) (json.RawMessage, error) { |
| 25 | h.metrics.callsTotal.Add(1) |
| 26 | start := time.Now() |
| 27 | raw, err := h.doCall(ctx, nodeID, method, reqBody) |
| 28 | h.metrics.recordCallLatency(time.Since(start)) |
| 29 | if err != nil { |
| 30 | h.metrics.callsErrTotal.Add(1) |
| 31 | if errors.Is(err, ErrNodeOffline) { |
| 32 | h.metrics.callsOfflineTotal.Add(1) |
| 33 | } |
| 34 | } |
| 35 | return raw, err |
| 36 | } |
| 37 | |
| 38 | func (h *Hub) doCall(ctx context.Context, nodeID, method string, reqBody any) (json.RawMessage, error) { |
| 39 | h.mu.RLock() |
nothing calls this directly
no test coverage detected