Skip to content

Commit

Permalink
[release-v1.5] rpcwebsocket: Use nonblocking messages and ntfns.
Browse files Browse the repository at this point in the history
All sends to channels that are serviced by separate goroutines that can
be shutdown via a quit channel need to ensure they select across that
quit channel when sending to the associated channel to ensure they can't
end up blocking on the send during shutdown.
  • Loading branch information
davecgh committed Jan 28, 2020
1 parent d5e6904 commit 5c985d9
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions rpcwebsocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -1992,7 +1992,15 @@ func (c *wsClient) SendMessage(marshalledJSON []byte, doneChan chan bool) {
return
}

c.sendChan <- wsResponse{msg: marshalledJSON, doneChan: doneChan}
// Use select statement to unblock enqueuing the message once the client has
// begun shutting down.
select {
case c.sendChan <- wsResponse{msg: marshalledJSON, doneChan: doneChan}:
case <-c.quit:
if doneChan != nil {
doneChan <- false
}
}
}

// ErrClientQuit describes the error where a client send is not processed due
Expand All @@ -2014,7 +2022,14 @@ func (c *wsClient) QueueNotification(marshalledJSON []byte) error {
return ErrClientQuit
}

c.ntfnChan <- marshalledJSON
// Use select statement to unblock enqueuing the message once the client has
// begun shutting down.
select {
case c.ntfnChan <- marshalledJSON:
case <-c.quit:
return ErrClientQuit
}

return nil
}

Expand Down

0 comments on commit 5c985d9

Please sign in to comment.