Compare commits

...

4 commits

Author SHA1 Message Date
3960216647 feat: introduce const for broadcast interval
- change ticker to use BROADCAST_INTERVAL constant
- define BROADCAST_INTERVAL as 1 second

🤖
2024-11-21 22:58:06 +01:00
7f034b2c4e 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

🤖
2024-11-21 22:58:06 +01:00
f83cf5bcc9 refactor: merge SendMessage() into SendPermanentBroadCastMessage() 2024-11-21 22:58:06 +01:00
c31b994d58 fix: add more Mutex Locks for Clients map 2024-11-21 21:50:25 +01:00
4 changed files with 10 additions and 11 deletions

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(BROADCAST_INTERVAL * 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 {
@ -22,14 +23,15 @@ func SendPermanentBroadCastMessage() {
} }
// Iterate over all connected clients and broadcast the message // Iterate over all connected clients and broadcast the message
mu.Lock()
for _, client := range Clients { for _, client := range Clients {
// Send message to client // Send message to client
err := client.SendMessage(websocket.TextMessage, jsonMessage) client.Conn.SetWriteDeadline(time.Now().Add(SEND_TIMEOUT * time.Second))
err := client.Conn.WriteMessage(websocket.TextMessage, jsonMessage)
if err != nil { if err != nil {
helper.Logger.Error("Error broadcasting to client:", "msg", err, "host", client.RealIP, "clients", len(Clients)) helper.Logger.Error("Error broadcasting to client:", "msg", err, "host", client.RealIP, "clients", len(Clients))
} }
} }
mu.Unlock()
<-tick.C
} }
} }

View file

@ -22,7 +22,9 @@ func handleClientCommands(c models.WebsocketClient) {
_, message, err := ws.ReadMessage() _, message, err := ws.ReadMessage()
if err != nil { if err != nil {
// remove client on error/disconnect // remove client on error/disconnect
mu.Lock()
delete(Clients, ws.LocalAddr()) delete(Clients, ws.LocalAddr())
mu.Unlock()
helper.Logger.Info("Client disconnected:", "msg", err, "host", c.RealIP, "clients", len(Clients)) helper.Logger.Info("Client disconnected:", "msg", err, "host", c.RealIP, "clients", len(Clients))
break break
} }

View file

@ -7,6 +7,7 @@ import (
"git.smsvc.net/pomodoro/GoTomato/pkg/models" "git.smsvc.net/pomodoro/GoTomato/pkg/models"
) )
const BROADCAST_INTERVAL = 1
const SEND_TIMEOUT = 10 const SEND_TIMEOUT = 10
const STALE_CLIENT_TIMEOUT = 90 const STALE_CLIENT_TIMEOUT = 90
const STALE_CHECK_INTERVALL = 30 const STALE_CHECK_INTERVALL = 30

View file

@ -19,9 +19,3 @@ type WebsocketClient struct {
LastPong time.Time LastPong time.Time
RealIP string RealIP string
} }
// Sends a message to the websocket.
func (c *WebsocketClient) SendMessage(messageType int, data []byte) error {
c.Conn.SetWriteDeadline(time.Now().Add(TIMEOUT * time.Second))
return c.Conn.WriteMessage(messageType, data)
}