Skip to content

Commit

Permalink
Merge 3d80d3a into 14685b6
Browse files Browse the repository at this point in the history
  • Loading branch information
Kr0llx authored Mar 1, 2024
2 parents 14685b6 + 3d80d3a commit 6d40b8e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
26 changes: 17 additions & 9 deletions ws/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,29 +269,37 @@ func (w *WebsocketConnection) writeMessage(messageType int, data []byte) bool {
return false
}

w.muxConWrite.Lock()
defer w.muxConWrite.Unlock()

err := w.conn.WriteMessage(messageType, data)
err := w.writeMessageWithoutErrorHandling(messageType, data)
if err != nil {
// ignore write errors if the connection got closed
w.closeWithError(err, "error writing to websocket: ")
logging.Log().Debug("WRITE ERROR: ", err)
return false
}

return true
}

// shutdown the connection and all internals
func (w *WebsocketConnection) CloseDataConnection(closeCode int, reason string) {
// make sure websocket Write is only called once at a time
func (w *WebsocketConnection) writeMessageWithoutErrorHandling(messageType int, data []byte) error {
if w.isConnClosed() {
return
return errors.New(connIsClosedError)
}

w.muxConWrite.Lock()
defer w.muxConWrite.Unlock()

return w.conn.WriteMessage(messageType, data)
}

// shutdown the connection and all internals
func (w *WebsocketConnection) CloseDataConnection(closeCode int, reason string) {

// send a close message to the remote side if we have a reason
if reason != "" {
_ = w.writeMessage(websocket.CloseMessage, websocket.FormatCloseMessage(closeCode, reason))
_ = w.writeMessageWithoutErrorHandling(websocket.CloseMessage, websocket.FormatCloseMessage(closeCode, reason))
}
w.setConnClosedError(nil)

w.close()
}

Expand Down
3 changes: 3 additions & 0 deletions ws/websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ func (s *WebsocketSuite) TestConnectionInvalid() {
result := s.sut.writeMessage(websocket.BinaryMessage, []byte{})
assert.Equal(s.T(), false, result)

err = s.sut.writeMessageWithoutErrorHandling(websocket.BinaryMessage, []byte{})
assert.NotNil(s.T(), err)

s.sut.conn = nil

data, err := s.sut.readWebsocketMessage()
Expand Down

0 comments on commit 6d40b8e

Please sign in to comment.