Skip to content

Commit

Permalink
Secure client writes using mutex
Browse files Browse the repository at this point in the history
Signed-off-by: Chaitanya Munukutla <[email protected]>
  • Loading branch information
c16a committed Apr 7, 2024
1 parent 9882dbd commit 20a5337
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 0 deletions.
3 changes: 3 additions & 0 deletions broker/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ func NewConnectedClient(conn WebSocketConnection, id string) *ConnectedClient {
}

func (client *ConnectedClient) WriteDataMessage(data []byte) error {
client.mutex.Lock()
defer client.mutex.Unlock()

return client.conn.WriteMessage(websocket.TextMessage, data)
}

Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func echo(upgrader websocket.Upgrader, b *broker.Broker, sp storage.Provider) fu
defer c.Close()

clientId := request.Header.Get("Client-Id")

client := broker.NewConnectedClient(c, clientId)
b.Connect(clientId, client)

Expand Down

0 comments on commit 20a5337

Please sign in to comment.