From 0ab0884508634cf5ae0957af4c2293bba98feead Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Wed, 20 Nov 2024 21:51:12 +0100 Subject: [PATCH] add more Mutex Locks for Clients map --- internal/websocket/broadcast.go | 2 ++ internal/websocket/client_commands.go | 2 ++ 2 files changed, 4 insertions(+) diff --git a/internal/websocket/broadcast.go b/internal/websocket/broadcast.go index f84c499..2decfb0 100644 --- a/internal/websocket/broadcast.go +++ b/internal/websocket/broadcast.go @@ -21,6 +21,7 @@ func SendPermanentBroadCastMessage() { } // Iterate over all connected clients and broadcast the message + mu.Lock() for _, client := range Clients { // Send message to client err := client.SendMessage(websocket.TextMessage, jsonMessage) @@ -28,6 +29,7 @@ func SendPermanentBroadCastMessage() { helper.Logger.Info("Error broadcasting to client:", "msg", err, "host", client.RealIP, "clients", len(Clients)) } } + mu.Unlock() <-tick.C } diff --git a/internal/websocket/client_commands.go b/internal/websocket/client_commands.go index 6f85108..7712d08 100644 --- a/internal/websocket/client_commands.go +++ b/internal/websocket/client_commands.go @@ -22,7 +22,9 @@ func handleClientCommands(c models.WebsocketClient) { _, message, err := ws.ReadMessage() if err != nil { // remove client on error/disconnect + mu.Lock() delete(Clients, ws.LocalAddr()) + mu.Unlock() helper.Logger.Info("Client disconnected:", "msg", err, "host", c.RealIP, "clients", len(Clients)) break }