Skip to content

Commit f1a0b26

Browse files
author
刘林欣
committed
fix websocket routine exiting
1 parent ecb0f58 commit f1a0b26

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

websocket.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,10 @@ func (c *wsConn) handleWsConn(ctx context.Context) {
545545
var timeoutCh <-chan time.Time
546546
if timeoutTimer != nil {
547547
if !timeoutTimer.Stop() {
548-
<-timeoutTimer.C
548+
select {
549+
case <-timeoutTimer.C:
550+
default:
551+
}
549552
}
550553
timeoutTimer.Reset(c.timeout)
551554

@@ -556,17 +559,14 @@ func (c *wsConn) handleWsConn(ctx context.Context) {
556559
case r, ok := <-c.incoming:
557560
if !ok {
558561
if c.incomingErr != nil {
559-
if !websocket.IsCloseError(c.incomingErr, websocket.CloseNormalClosure) {
560-
log.Debugw("websocket error", "error", c.incomingErr)
562+
log.Debugw("websocket error", "error", c.incomingErr)
563+
// only client needs to reconnect
564+
if c.connFactory != nil {
561565
// connection dropped unexpectedly, do our best to recover it
562566
c.closeInFlight()
563567
c.closeChans()
564568
c.incoming = make(chan io.Reader) // listen again for responses
565569
go func() {
566-
if c.connFactory == nil { // likely the server side, don't try to reconnect
567-
return
568-
}
569-
570570
stopPings()
571571

572572
attempts := 0
@@ -652,7 +652,12 @@ func (c *wsConn) handleWsConn(ctx context.Context) {
652652
}
653653
c.writeLk.Unlock()
654654
log.Errorw("Connection timeout", "remote", c.conn.RemoteAddr())
655-
return
655+
// The server side does not perform the reconnect operation, so need to exit
656+
if c.connFactory == nil {
657+
return
658+
}
659+
// The client performs the reconnect operation, and if it exits it cannot start a handleWsConn again, so it does not need to exit
660+
continue
656661
case <-c.stop:
657662
c.writeLk.Lock()
658663
cmsg := websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")

0 commit comments

Comments
 (0)