-
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Prevent concurrent WS socket write errors in the CDS client
- Loading branch information
Showing
3 changed files
with
44 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package util | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/gorilla/websocket" | ||
) | ||
|
||
// Conn represents a client WebSocket connection. An added lock guards the underlying connection | ||
// from concurrent write to websocket connection errors. | ||
type Conn struct { | ||
*websocket.Conn | ||
readLock, writeLock sync.Mutex // for writemessage | ||
|
||
} | ||
|
||
// NewConn wraps a WebSocket connection. | ||
func NewConn(conn *websocket.Conn) *Conn { | ||
return &Conn{Conn: conn} | ||
} | ||
|
||
// WriteMessage writes a message to the client connection with proper locking. | ||
func (c *Conn) WriteMessage(messageType int, data []byte) error { | ||
c.writeLock.Lock() | ||
defer c.writeLock.Unlock() | ||
return c.Conn.WriteMessage(messageType, data) | ||
} | ||
|
||
// ReadMessage reads a message from the client connection with proper locking. | ||
func (c *Conn) ReadMessage() (int, []byte, error) { | ||
c.readLock.Lock() | ||
defer c.readLock.Unlock() | ||
return c.Conn.ReadMessage() | ||
} |