Skip to content

Commit

Permalink
add context life cycle control.
Browse files Browse the repository at this point in the history
  • Loading branch information
mo3et committed Jul 9, 2024
1 parent 557a5a3 commit 1b1bc0b
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions internal/msggateway/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,15 @@ func (c *Client) readMessage() {
if returnErr != nil {
log.ZWarn(c.ctx, "readMessage", returnErr, "messageType", messageType)
c.closedErr = returnErr
<-c.ctx.Done()
return
}

log.ZDebug(c.ctx, "readMessage", "messageType", messageType)
if c.closed.Load() {
// The scenario where the connection has just been closed, but the coroutine has not exited
c.closedErr = ErrConnClosed
<-c.ctx.Done()
return
}

Expand All @@ -154,10 +156,12 @@ func (c *Client) readMessage() {
parseDataErr := c.handleMessage(message)
if parseDataErr != nil {
c.closedErr = parseDataErr
<-c.ctx.Done()
return
}
case MessageText:
c.closedErr = ErrNotSupportMessageProtocol
<-c.ctx.Done()
return

case PingMessage:
Expand All @@ -166,7 +170,9 @@ func (c *Client) readMessage() {

case CloseMessage:
c.closedErr = ErrClientClosed
<-c.ctx.Done()
return

default:
}
}
Expand Down

0 comments on commit 1b1bc0b

Please sign in to comment.