Skip to content

Commit 947efd9

Browse files
committed
rpc: Atomic batch ID counter
1 parent 2b5eb01 commit 947efd9

File tree

4 files changed

+19
-12
lines changed

4 files changed

+19
-12
lines changed

internal/http.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ func IncomingHttpHandler(ctx context.Context, endpoint *Endpoint, w http.Respons
142142
}
143143

144144
if req.IsBatch {
145-
rpc.BatchIDCounter++
146-
log = log.With("batch_id", rpc.BatchIDCounter, "batch_size", len(req.Requests))
145+
batchId := rpc.BatchIDCounter.Add(1)
146+
log = log.With("batch_id", batchId, "batch_size", len(req.Requests))
147147
} else {
148148
log = log.With("rpc_id", req.Requests[0].GetID(), "method", req.Requests[0].Method)
149149
}

internal/rpc/response.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ type Response struct {
99
Version string `json:"jsonrpc"`
1010

1111
// ID might be a string or number
12-
ID any `json:"id,omitempty"`
13-
Result any `json:"result"`
14-
Error any `json:"error,omitempty"`
12+
ID any `json:"id,omitempty"`
13+
Result any `json:"result"`
14+
Method string `json:"method"`
15+
Params any `json:"params"`
16+
Error any `json:"error,omitempty"`
1517
}
1618

1719
func (r *Response) GetID() string {

internal/rpc/rpc.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import (
44
"context"
55
"strconv"
66
"strings"
7+
"sync/atomic"
78
)
89

910
type ReaderFunc func(ctx context.Context, req *Request, errRpcError bool) (*Response, error)
1011

11-
var BatchIDCounter = 0
12+
var BatchIDCounter atomic.Uint64
1213

1314
// GetRequestIDString returns the request ID as a string.
1415
// The request ID is commonly a number, but might be a string with a number or a string.

internal/ws.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -244,15 +244,19 @@ func (proxy *WebSocketProxy) pumpClient(client *Client) {
244244
requestLog := proxy.log
245245

246246
if req.IsBatch {
247-
rpc.BatchIDCounter++
248-
249-
requestLog = requestLog.With("batch_id", rpc.BatchIDCounter, "batch_size", len(req.Requests))
247+
batchId := rpc.BatchIDCounter.Add(1)
248+
requestLog = requestLog.With("batch_id", batchId, "batch_size", len(req.Requests))
250249
}
251250

252251
// Break up any batched requests into one request per message
253-
for i, req := range req.Requests {
254-
proxy.Requests <- req
255-
requestLog.Debug("request", "batch_index", i, "rpc_id", req.GetID(), "method", req.Method)
252+
for i, r := range req.Requests {
253+
proxy.Requests <- r
254+
255+
if req.IsBatch {
256+
requestLog.With("batch_index", i, "rpc_id", r.GetID(), "method", r.Method).Debug("request")
257+
} else {
258+
requestLog.With("rpc_id", r.GetID(), "method", r.Method).Debug("request")
259+
}
256260
}
257261

258262
case rpcResponse, ok := <-proxy.Responses:

0 commit comments

Comments
 (0)