feat: improve broadcast loop ticker

- 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

🤖
This commit is contained in:
Sebastian Mark 2024-11-21 18:49:10 +01:00
parent f83cf5bcc9
commit 7f034b2c4e

View file

@ -11,9 +11,10 @@ import (
// Sends continous messages to all connected WebSocket clients // Sends continous messages to all connected WebSocket clients
func SendPermanentBroadCastMessage() { 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 // Marshal the message into JSON format
jsonMessage, err := json.Marshal(shared.State) jsonMessage, err := json.Marshal(shared.State)
if err != nil { if err != nil {
@ -32,7 +33,5 @@ func SendPermanentBroadCastMessage() {
} }
} }
mu.Unlock() mu.Unlock()
<-tick.C
} }
} }