From efc34b57b2a058cab115f70d61a0019e48b1d823 Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Thu, 21 Nov 2024 18:49:10 +0100 Subject: [PATCH] feat: improve broadcast message handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - rename tick to ticker for clarity - change infinite loop to range over ticker channel - add defer statement to stop the ticker properly - remove unnecessary channel receive operation 🤖 --- internal/websocket/broadcast.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/internal/websocket/broadcast.go b/internal/websocket/broadcast.go index 57f4449..e2c175d 100644 --- a/internal/websocket/broadcast.go +++ b/internal/websocket/broadcast.go @@ -11,9 +11,10 @@ import ( // Sends continous messages to all connected WebSocket clients func SendPermanentBroadCastMessage() { - tick := time.NewTicker(time.Second) + ticker := time.NewTicker(time.Second) + defer ticker.Stop() - for { + for range ticker.C { // Marshal the message into JSON format jsonMessage, err := json.Marshal(shared.State) if err != nil { @@ -32,7 +33,5 @@ func SendPermanentBroadCastMessage() { } } mu.Unlock() - - <-tick.C } }