@@ -105,6 +105,8 @@ type Client struct {
105105 reqInit chan * requestOp // register response IDs, takes write lock
106106 reqSent chan error // signals write completion, releases write lock
107107 reqTimeout chan * requestOp // removes response IDs when call timeout expires
108+
109+ recorder Recorder // optional, may be nil
108110}
109111
110112type reconnectFunc func (context.Context ) (ServerCodec , error )
@@ -121,6 +123,7 @@ func (c *Client) newClientConn(conn ServerCodec) *clientConn {
121123 ctx = context .WithValue (ctx , clientContextKey {}, c )
122124 ctx = context .WithValue (ctx , peerInfoContextKey {}, conn .peerInfo ())
123125 handler := newHandler (ctx , conn , c .idgen , c .services , c .batchItemLimit , c .batchResponseMaxSize )
126+ handler .recorder = c .recorder
124127 return & clientConn {conn , handler }
125128}
126129
@@ -258,6 +261,7 @@ func initClient(conn ServerCodec, services *serviceRegistry, cfg *clientConfig)
258261 reqInit : make (chan * requestOp ),
259262 reqSent : make (chan error , 1 ),
260263 reqTimeout : make (chan * requestOp ),
264+ recorder : cfg .recorder ,
261265 }
262266
263267 // Set defaults.
@@ -343,6 +347,10 @@ func (c *Client) CallContext(ctx context.Context, result interface{}, method str
343347 if err != nil {
344348 return err
345349 }
350+ var recordDone RecordDone
351+ if c .recorder != nil {
352+ recordDone = c .recorder .RecordOutgoing (ctx , msg )
353+ }
346354 op := & requestOp {
347355 ids : []json.RawMessage {msg .ID },
348356 resp : make (chan []* jsonrpcMessage , 1 ),
@@ -363,6 +371,9 @@ func (c *Client) CallContext(ctx context.Context, result interface{}, method str
363371 return err
364372 }
365373 resp := batchresp [0 ]
374+ if recordDone != nil {
375+ recordDone (ctx , msg , resp )
376+ }
366377 switch {
367378 case resp .Error != nil :
368379 return resp .Error
@@ -415,7 +426,13 @@ func (c *Client) BatchCallContext(ctx context.Context, b []BatchElem) error {
415426 op .ids [i ] = msg .ID
416427 byID [string (msg .ID )] = i
417428 }
418-
429+ var recordDone []RecordDone
430+ if c .recorder != nil {
431+ recordDone = make ([]RecordDone , len (b ))
432+ for i , msg := range msgs {
433+ recordDone [i ] = c .recorder .RecordOutgoing (ctx , msg )
434+ }
435+ }
419436 var err error
420437 if c .isHTTP {
421438 err = c .sendBatchHTTP (ctx , op , msgs )
@@ -446,6 +463,10 @@ func (c *Client) BatchCallContext(ctx context.Context, b []BatchElem) error {
446463 }
447464 delete (byID , string (resp .ID ))
448465
466+ if recordDone != nil {
467+ recordDone [index ](ctx , msgs [index ], resp )
468+ }
469+
449470 // Assign result and error.
450471 elem := & b [index ]
451472 switch {
@@ -462,6 +483,9 @@ func (c *Client) BatchCallContext(ctx context.Context, b []BatchElem) error {
462483 for _ , index := range byID {
463484 elem := & b [index ]
464485 elem .Error = ErrMissingBatchResponse
486+ if recordDone != nil {
487+ recordDone [index ](ctx , msgs [index ], nil )
488+ }
465489 }
466490
467491 return err
0 commit comments