Skip to content

Commit 0e65da3

Browse files
committed
Debug trace option in websocket loop
1 parent 865b687 commit 0e65da3

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

rpc_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ func init() {
2727
if err := logging.SetLogLevel("rpc", "DEBUG"); err != nil {
2828
panic(err)
2929
}
30+
31+
debugTrace = true
3032
}
3133

3234
type SimpleServerHandler struct {

websocket.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"io"
99
"io/ioutil"
10+
"os"
1011
"reflect"
1112
"sync"
1213
"sync/atomic"
@@ -20,6 +21,8 @@ const wsCancel = "xrpc.cancel"
2021
const chValue = "xrpc.ch.val"
2122
const chClose = "xrpc.ch.close"
2223

24+
var debugTrace = os.Getenv("JSONRPC_ENABLE_DEBUG_TRACE") == "1"
25+
2326
type frame struct {
2427
// common
2528
Jsonrpc string `json:"jsonrpc"`
@@ -132,6 +135,10 @@ func (c *wsConn) sendRequest(req request) error {
132135
c.writeLk.Lock()
133136
defer c.writeLk.Unlock()
134137

138+
if debugTrace {
139+
log.Debugw("sendRequest", "req", req.Method, "id", req.ID)
140+
}
141+
135142
if err := c.conn.WriteJSON(req); err != nil {
136143
return err
137144
}
@@ -622,7 +629,7 @@ func (c *wsConn) handleWsConn(ctx context.Context) {
622629
var frame frame
623630
if err = json.NewDecoder(r).Decode(&frame); err == nil {
624631
if frame.ID, err = normalizeID(frame.ID); err == nil {
625-
action = fmt.Sprintf("incoming(%s,%s)", frame.Method, frame.ID)
632+
action = fmt.Sprintf("incoming(%s,%v)", frame.Method, frame.ID)
626633

627634
c.handleFrame(ctx, frame)
628635
go c.nextMessage()
@@ -641,7 +648,7 @@ func (c *wsConn) handleWsConn(ctx context.Context) {
641648
return // failed to reconnect
642649
}
643650
case req := <-c.requests:
644-
action = fmt.Sprintf("send-request(%s)", req.req.Method)
651+
action = fmt.Sprintf("send-request(%s,%v)", req.req.Method, req.req.ID)
645652

646653
c.writeLk.Lock()
647654
if req.req.ID != nil {
@@ -701,6 +708,9 @@ func (c *wsConn) handleWsConn(ctx context.Context) {
701708
if c.pingInterval > 0 && time.Since(start) > c.pingInterval*2 {
702709
log.Warnw("websocket long time no response", "lastAction", action, "time", time.Since(start))
703710
}
711+
if debugTrace {
712+
log.Debugw("websocket action", "lastAction", action, "time", time.Since(start))
713+
}
704714
}
705715
}
706716

0 commit comments

Comments
 (0)