Skip to content

Commit

Permalink
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 Dec 27, 2019
1 parent eb7bc29 commit 1cfd572
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions rpcwebsocket.go
Expand Up @@ -1993,7 +1993,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 @@ -2015,7 +2023,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 1cfd572

Please sign in to comment.