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() { ticker := time.NewTicker(STALE_CHECK_INTERVALL * time.Second) defer ticker.Stop() for range ticker.C { mu.Lock() for _, client := range Clients { client.Conn.SetWriteDeadline(time.Now().Add(SEND_TIMEOUT * time.Second)) client.Conn.WriteMessage(websocket.PingMessage, nil) if isStale(client) { client.Conn.Close() delete(Clients, client.Conn.LocalAddr()) helper.Logger.Info("Removed stale client", "host", client.RealIP, "lastPong", client.LastPong.Format("RFC3339")) } } mu.Unlock() } } func isStale(client *models.WebsocketClient) bool { return time.Since(client.LastPong) > (STALE_CLIENT_TIMEOUT * time.Second) }