From f13bdff03f354b67ce3b20ca40b16d50ae012f16 Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Thu, 21 Nov 2024 18:49:10 +0100 Subject: [PATCH] feat: improve broadcast loop ticker 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 } }