Skip to content

Commit 666479d

Browse files
committed
fix handling of types.
1 parent 34adecb commit 666479d

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

client.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func httpClient(ctx context.Context, addr string, namespace string, outs []inter
171171
return clientResponse{}, xerrors.Errorf("http status %s unmarshaling response: %w", httpResp.Status, err)
172172
}
173173

174-
if resp.ID, err = translateID(resp.ID); err != nil {
174+
if resp.ID, err = normalizeID(resp.ID); err != nil {
175175
return clientResponse{}, xerrors.Errorf("failed to response ID: %w", err)
176176
}
177177

@@ -471,7 +471,7 @@ func (fn *rpcFunc) processError(err error) []reflect.Value {
471471
}
472472

473473
func (fn *rpcFunc) handleRpcCall(args []reflect.Value) (results []reflect.Value) {
474-
id := atomic.AddInt64(&fn.client.idCtr, 1)
474+
var id interface{} = atomic.AddInt64(&fn.client.idCtr, 1)
475475
params := make([]param, len(args)-fn.hasCtx)
476476
for i, arg := range args[fn.hasCtx:] {
477477
enc, found := fn.client.paramEncoders[arg.Type()]
@@ -506,6 +506,16 @@ func (fn *rpcFunc) handleRpcCall(args []reflect.Value) (results []reflect.Value)
506506
retVal, chCtor = fn.client.makeOutChan(ctx, fn.ftyp, fn.valOut)
507507
}
508508

509+
// Prepare the ID to send on the wire.
510+
// We track int64 ids as float64 in the inflight map (because that's what
511+
// they'll be decoded to). encoding/json outputs numbers with their minimal
512+
// encoding, avoding the decimal point when possible, i.e. 3 will never get
513+
// converted to 3.0.
514+
id, err := normalizeID(id)
515+
if err != nil {
516+
return fn.processError(fmt.Errorf("failed to normalize id")) // should probably panic
517+
}
518+
509519
req := request{
510520
Jsonrpc: "2.0",
511521
ID: id,
@@ -529,7 +539,6 @@ func (fn *rpcFunc) handleRpcCall(args []reflect.Value) (results []reflect.Value)
529539
}
530540

531541
var resp clientResponse
532-
var err error
533542
// keep retrying if got a forced closed websocket conn and calling method
534543
// has retry annotation
535544
for attempt := 0; true; attempt++ {

handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func (s *RPCServer) handleReader(ctx context.Context, r io.Reader, w io.Writer,
174174
return
175175
}
176176

177-
if req.ID, err = translateID(req.ID); err != nil {
177+
if req.ID, err = normalizeID(req.ID); err != nil {
178178
rpcError(wf, &req, rpcParseError, xerrors.Errorf("failed to parse ID: %w", err))
179179
return
180180
}

rpc_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1130,7 +1130,7 @@ func TestIDHandling(t *testing.T) {
11301130
t.Run(fmt.Sprintf("%v", tc.expect), func(t *testing.T) {
11311131
dec := json.NewDecoder(strings.NewReader(tc.str))
11321132
require.NoError(t, dec.Decode(&decoded))
1133-
if id, err := translateID(decoded.ID); !tc.expectErr {
1133+
if id, err := normalizeID(decoded.ID); !tc.expectErr {
11341134
require.NoError(t, err)
11351135
require.Equal(t, tc.expect, id)
11361136
} else {

websocket.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ func (c *wsConn) cancelCtx(req frame) {
297297
log.Warnf("%s call with ID set, won't respond", wsCancel)
298298
}
299299

300-
var id int64
300+
var id interface{}
301301
if err := json.Unmarshal(req.Params[0].data, &id); err != nil {
302302
log.Error("handle me:", err)
303303
return
@@ -610,7 +610,7 @@ func (c *wsConn) handleWsConn(ctx context.Context) {
610610

611611
var frame frame
612612
if err = json.NewDecoder(r).Decode(&frame); err == nil {
613-
if frame.ID, err = translateID(frame.ID); err == nil {
613+
if frame.ID, err = normalizeID(frame.ID); err == nil {
614614
c.handleFrame(ctx, frame)
615615
go c.nextMessage()
616616
continue
@@ -687,11 +687,14 @@ func (c *wsConn) handleWsConn(ctx context.Context) {
687687
}
688688
}
689689

690-
// Takes an ID as received on the wire, validates it, and translates it to a normalized ID appropriate for keying.
691-
func translateID(id interface{}) (interface{}, error) {
690+
// Takes an ID as received on the wire, validates it, and translates it to a
691+
// normalized ID appropriate for keying.
692+
func normalizeID(id interface{}) (interface{}, error) {
692693
switch v := id.(type) {
693694
case string, float64, nil:
694695
return v, nil
696+
case int64: // clients sending int64 need to normalize to float64
697+
return float64(v), nil
695698
default:
696699
return nil, xerrors.Errorf("invalid id type: %T", id)
697700
}

0 commit comments

Comments
 (0)