From c76ea3628b5468225df49eac873c471486adcac9 Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Thu, 21 Nov 2024 08:44:50 +0100 Subject: [PATCH] add timeout consts in new file --- internal/websocket/broadcast.go | 1 + internal/websocket/staleClients.go | 6 +++--- internal/websocket/timeouts.go | 5 +++++ pkg/models/client.go | 2 -- 4 files changed, 9 insertions(+), 5 deletions(-) create mode 100644 internal/websocket/timeouts.go diff --git a/internal/websocket/broadcast.go b/internal/websocket/broadcast.go index 2909da1..133f366 100644 --- a/internal/websocket/broadcast.go +++ b/internal/websocket/broadcast.go @@ -12,6 +12,7 @@ import ( // Sends continous messages to all connected WebSocket clients func SendPermanentBroadCastMessage() { tick := time.NewTicker(time.Second) + for { // Marshal the message into JSON format jsonMessage, err := json.Marshal(shared.State) diff --git a/internal/websocket/staleClients.go b/internal/websocket/staleClients.go index 247af04..a4a9a31 100644 --- a/internal/websocket/staleClients.go +++ b/internal/websocket/staleClients.go @@ -8,7 +8,7 @@ import ( ) func sendPing(client *models.WebsocketClient) bool { - client.Conn.SetWriteDeadline(time.Now().Add(10 * time.Second)) + client.Conn.SetWriteDeadline(time.Now().Add(SEND_TIMEOUT * time.Second)) err := client.Conn.WriteMessage(websocket.PingMessage, nil) if err != nil { @@ -19,12 +19,12 @@ func sendPing(client *models.WebsocketClient) bool { } func isStale(client *models.WebsocketClient) bool { - return time.Since(client.LastPong) > (90 * time.Second) + return time.Since(client.LastPong) > (STALE_CLIENT_TIMEOUT * time.Second) } // Check and remove stale clients func RemoveStaleClients() { - ticker := time.NewTicker(30 * time.Second) + ticker := time.NewTicker(STALE_CHECK_INTERVALL * time.Second) defer ticker.Stop() for range ticker.C { diff --git a/internal/websocket/timeouts.go b/internal/websocket/timeouts.go new file mode 100644 index 0000000..4a926f9 --- /dev/null +++ b/internal/websocket/timeouts.go @@ -0,0 +1,5 @@ +package websocket + +const SEND_TIMEOUT = 10 +const STALE_CLIENT_TIMEOUT = 90 +const STALE_CHECK_INTERVALL = 30 diff --git a/pkg/models/client.go b/pkg/models/client.go index 55d3aae..9d5dbdb 100644 --- a/pkg/models/client.go +++ b/pkg/models/client.go @@ -6,8 +6,6 @@ import ( "github.com/gorilla/websocket" ) -const TIMEOUT = 10 - // Represents a command from the client (start/stop) type ClientCommand struct { Command string `json:"command"` // Command send to the server