@@ -50,6 +50,12 @@ type Handler func(context.Context, *Conn, *Request)
5050// instead.
5151type Canceler func (context.Context , * Conn , * Request )
5252
53+ type handling struct {
54+ request * Request
55+ cancel context.CancelFunc
56+ start time.Time
57+ }
58+
5359// Conn is a JSON RPC 2 client server connection.
5460// Conn is bidirectional; it does not have a designated server or client end.
5561type Conn struct {
@@ -60,20 +66,14 @@ type Conn struct {
6066 Capacity int
6167 RejectIfOverloaded bool
6268 stream Stream
63- pendingMu sync.Mutex // protects the pending map
6469 pending map [ID ]chan * Response
65- handlingMu sync.Mutex // protects the handling map
70+ pendingMu sync.Mutex // protects the pending map
6671 handling map [ID ]handling
72+ handlingMu sync.Mutex // protects the handling map
6773}
6874
6975var _ Interface = (* Conn )(nil )
7076
71- type queueEntry struct {
72- ctx context.Context
73- conn * Conn
74- request * Request
75- }
76-
7777// Options represents a functional options.
7878type Options func (* Conn )
7979
@@ -127,9 +127,6 @@ var defaultLogger = zap.NewNop()
127127func NewConn (s Stream , options ... Options ) * Conn {
128128 conn := & Conn {
129129 seq : new (atomic.Int64 ),
130- Handler : defaultHandler , // the default handler reports a method error
131- Canceler : defaultCanceler , // the default canceller does nothing
132- Logger : defaultLogger , // the default Logger does nothing
133130 stream : s ,
134131 pending : make (map [ID ]chan * Response ),
135132 handling : make (map [ID ]handling ),
@@ -139,6 +136,19 @@ func NewConn(s Stream, options ...Options) *Conn {
139136 opt (conn )
140137 }
141138
139+ // the default handler reports a method error
140+ if conn .Handler == nil {
141+ conn .Handler = defaultHandler
142+ }
143+ // the default canceller does nothing
144+ if conn .Canceler == nil {
145+ conn .Canceler = defaultCanceler
146+ }
147+ // the default Logger does nothing
148+ if conn .Logger == nil {
149+ conn .Logger = defaultLogger
150+ }
151+
142152 return conn
143153}
144154
@@ -156,7 +166,7 @@ func (c *Conn) Cancel(id ID) {
156166
157167// Notify is called to send a notification request over the connection.
158168func (c * Conn ) Notify (ctx context.Context , method string , params interface {}) error {
159- c .Logger .Debug ("Notify" )
169+ c .Logger .Debug ("Notify" , zap . String ( "method" , method ), zap . Any ( "params" , params ) )
160170 p , err := c .marshalInterface (params )
161171 if err != nil {
162172 return Errorf (CodeParseError , "failed to marshaling notify parameters: %v" , err )
@@ -187,7 +197,7 @@ func (c *Conn) Notify(ctx context.Context, method string, params interface{}) er
187197
188198// Call sends a request over the connection and then waits for a response.
189199func (c * Conn ) Call (ctx context.Context , method string , params , result interface {}) error {
190- c .Logger .Debug ("Call" )
200+ c .Logger .Debug ("Call" , zap . String ( "method" , method ), zap . Any ( "params" , params ) )
191201 p , err := c .marshalInterface (params )
192202 if err != nil {
193203 return Errorf (CodeParseError , "failed to marshaling call parameters: %v" , err )
@@ -218,7 +228,6 @@ func (c *Conn) Call(ctx context.Context, method string, params, result interface
218228 c .pendingMu .Unlock ()
219229 }()
220230
221- start := time .Now ()
222231 c .Logger .Debug (Send ,
223232 zap .String ("req.JSONRPC" , req .JSONRPC ),
224233 zap .String ("id" , id .String ()),
@@ -233,12 +242,8 @@ func (c *Conn) Call(ctx context.Context, method string, params, result interface
233242 // wait for the response
234243 select {
235244 case resp := <- rchan :
236- elapsed := time .Since (start )
237245 c .Logger .Debug (Receive ,
238- zap .String ("resp.ID" , resp .ID .String ()),
239- zap .Duration ("elapsed" , elapsed ),
240- zap .String ("req.method" , req .Method ),
241- zap .Any ("resp.Result" , resp .Result ),
246+ zap .Any ("resp" , resp ),
242247 )
243248
244249 // is it an error response?
@@ -331,24 +336,24 @@ func (c *Conn) Reply(ctx context.Context, req *Request, result interface{}, err
331336 return nil
332337}
333338
334- type handling struct {
339+ type queueEntry struct {
340+ ctx context.Context
341+ conn * Conn
335342 request * Request
336- cancel context.CancelFunc
337- start time.Time
338343}
339344
340- func (c * Conn ) deliver (ctx context.Context , q chan queueEntry , request * Request ) bool {
345+ func (c * Conn ) deliver (ctx context.Context , queuec chan queueEntry , request * Request ) bool {
341346 c .Logger .Debug ("deliver" )
342347
343348 e := queueEntry {ctx : ctx , conn : c , request : request }
344349
345350 if ! c .RejectIfOverloaded {
346- q <- e
351+ queuec <- e
347352 return true
348353 }
349354
350355 select {
351- case q <- e :
356+ case queuec <- e :
352357 return true
353358 default :
354359 return false
@@ -357,16 +362,15 @@ func (c *Conn) deliver(ctx context.Context, q chan queueEntry, request *Request)
357362
358363// Run run the jsonrpc2 server.
359364func (c * Conn ) Run (ctx context.Context ) (err error ) {
360- q := make (chan queueEntry , c .Capacity )
361- defer close (q )
365+ queuec := make (chan queueEntry , c .Capacity )
366+ defer close (queuec )
362367
363368 // start the queue processor
364369 go func () {
365- for e := range q {
370+ for e := range queuec {
366371 if e .ctx .Err () != nil {
367372 continue
368373 }
369- c .Logger .Debug ("c.Handler" , zap .Reflect ("e" , e .conn ), zap .Reflect ("e.request" , e .request ))
370374 c .Handler (e .ctx , e .conn , e .request )
371375 }
372376 }()
@@ -377,8 +381,6 @@ func (c *Conn) Run(ctx context.Context) (err error) {
377381 return err // read the stream failed, cannot continue
378382 }
379383
380- c .Logger .Debug (Receive , zap .ByteString ("data" , data ), zap .Int ("len(data)" , len (data )))
381-
382384 msg := & Combined {}
383385 if err := json .Unmarshal (data , msg ); err != nil { // TODO(zchee): use gojay
384386 // a badly formed message arrived, log it and continue
@@ -407,7 +409,7 @@ func (c *Conn) Run(ctx context.Context) (err error) {
407409 zap .Any ("req.Params" , req .Params ),
408410 )
409411 // add to the processor queue
410- c .deliver (ctx , q , req )
412+ c .deliver (ctx , queuec , req )
411413 // TODO: log when we drop a message?
412414 } else {
413415 // handle the Call, add to the processor queue.
@@ -425,7 +427,7 @@ func (c *Conn) Run(ctx context.Context) (err error) {
425427 zap .Any ("req.Params" , req .Params ),
426428 )
427429
428- if ! c .deliver (ctxReq , q , req ) {
430+ if ! c .deliver (ctxReq , queuec , req ) {
429431 // queue is full, reject the message by directly replying
430432 c .Reply (ctx , req , nil , Errorf (CodeServerOverloaded , "no room in queue" ))
431433 }
@@ -464,6 +466,6 @@ func (c *Conn) marshalInterface(obj interface{}) (*json.RawMessage, error) {
464466 return nil , err
465467 }
466468 raw := json .RawMessage (data )
467- c . Logger . Debug ( "marshalInterface" , zap . ByteString ( "raw" , raw ))
469+
468470 return & raw , nil
469471}
0 commit comments