GoTomato/internal/websocket/staleClients.go

35 lines
886 B
Go
Raw Normal View History

package websocket
import (
"time"
"git.smsvc.net/pomodoro/GoTomato/internal/helper"
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
"github.com/gorilla/websocket"
)
// Check and remove stale clients
func RemoveStaleClients() {
2024-11-21 07:44:50 +00:00
ticker := time.NewTicker(STALE_CHECK_INTERVALL * time.Second)
defer ticker.Stop()
for range ticker.C {
mu.Lock()
for _, client := range Clients {
2024-11-21 19:11:09 +00:00
client.Conn.SetWriteDeadline(time.Now().Add(SEND_TIMEOUT * time.Second))
client.Conn.WriteMessage(websocket.PingMessage, nil)
if isStale(client) {
client.Conn.Close()
2024-11-20 21:01:56 +00:00
delete(Clients, client.Conn.LocalAddr())
2024-11-21 19:15:33 +00:00
helper.Logger.Info("Removed stale client", "host", client.RealIP, "lastPong", client.LastPong.Format("RFC3339"))
}
}
mu.Unlock()
}
}
2024-11-21 08:34:19 +00:00
func isStale(client *models.WebsocketClient) bool {
return time.Since(client.LastPong) > (STALE_CLIENT_TIMEOUT * time.Second)
}