44 "context"
55 "encoding/json"
66 "errors"
7+ "fmt"
78 "io"
89 "io/ioutil"
910 "reflect"
@@ -92,11 +93,7 @@ type wsConn struct {
9293
9394// nextMessage wait for one message and puts it to the incoming channel
9495func (c * wsConn ) nextMessage () {
95- if c .timeout > 0 {
96- if err := c .conn .SetReadDeadline (time .Now ().Add (c .timeout )); err != nil {
97- log .Error ("setting read deadline" , err )
98- }
99- }
96+ c .resetReadDeadline ()
10097 msgType , r , err := c .conn .NextReader ()
10198 if err != nil {
10299 c .incomingErr = err
@@ -609,8 +606,13 @@ func (c *wsConn) handleWsConn(ctx context.Context) {
609606 timeoutCh = timeoutTimer .C
610607 }
611608
609+ start := time .Now ()
610+ action := ""
611+
612612 select {
613613 case r , ok := <- c .incoming :
614+ action = "incoming"
615+
614616 err := c .incomingErr
615617
616618 if ok {
@@ -620,9 +622,11 @@ func (c *wsConn) handleWsConn(ctx context.Context) {
620622 var frame frame
621623 if err = json .NewDecoder (r ).Decode (& frame ); err == nil {
622624 if frame .ID , err = normalizeID (frame .ID ); err == nil {
625+ action = fmt .Sprintf ("incoming(%s,%s)" , frame .Method , frame .ID )
626+
623627 c .handleFrame (ctx , frame )
624628 go c .nextMessage ()
625- continue
629+ break
626630 }
627631 }
628632 }
@@ -637,6 +641,8 @@ func (c *wsConn) handleWsConn(ctx context.Context) {
637641 return // failed to reconnect
638642 }
639643 case req := <- c .requests :
644+ action = fmt .Sprintf ("send-request(%s)" , req .req .Method )
645+
640646 c .writeLk .Lock ()
641647 if req .req .ID != nil {
642648 if c .incomingErr != nil { // No conn?, immediate fail
@@ -658,11 +664,9 @@ func (c *wsConn) handleWsConn(ctx context.Context) {
658664 log .Errorf ("sendReqest failed (Handle me): %s" , err )
659665 }
660666 case <- c .pongs :
661- if c .timeout > 0 {
662- if err := c .conn .SetReadDeadline (time .Now ().Add (c .timeout )); err != nil {
663- log .Error ("setting read deadline" , err )
664- }
665- }
667+ action = "pong"
668+
669+ c .resetReadDeadline ()
666670 case <- timeoutCh :
667671 if c .pingInterval == 0 {
668672 // pings not running, this is perfectly normal
@@ -674,7 +678,7 @@ func (c *wsConn) handleWsConn(ctx context.Context) {
674678 log .Warnw ("timed-out websocket close error" , "error" , err )
675679 }
676680 c .writeLk .Unlock ()
677- log .Errorw ("Connection timeout" , "remote" , c .conn .RemoteAddr ())
681+ log .Errorw ("Connection timeout" , "remote" , c .conn .RemoteAddr (), "lastAction" , action )
678682 // The server side does not perform the reconnect operation, so need to exit
679683 if c .connFactory == nil {
680684 return
@@ -693,6 +697,18 @@ func (c *wsConn) handleWsConn(ctx context.Context) {
693697 c .writeLk .Unlock ()
694698 return
695699 }
700+
701+ if c .pingInterval > 0 && time .Since (start ) > c .pingInterval * 2 {
702+ log .Warnw ("websocket long time no response" , "lastAction" , action , "time" , time .Since (start ))
703+ }
704+ }
705+ }
706+
707+ func (c * wsConn ) resetReadDeadline () {
708+ if c .timeout > 0 {
709+ if err := c .conn .SetReadDeadline (time .Now ().Add (c .timeout )); err != nil {
710+ log .Error ("setting read deadline" , err )
711+ }
696712 }
697713}
698714
0 commit comments