From 60ed8c3ca09cdecef135dfb8d6ec644e812636ab Mon Sep 17 00:00:00 2001 From: Salastil Date: Tue, 18 Nov 2025 20:21:58 -0500 Subject: [PATCH] Fix: Silent failure on reconnect, self healing via /join when chat is silent after an outage --- sneed/client.go | 95 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 72 insertions(+), 23 deletions(-) diff --git a/sneed/client.go b/sneed/client.go index 32028b2..3d79c0f 100644 --- a/sneed/client.go +++ b/sneed/client.go @@ -16,12 +16,16 @@ import ( ) const ( - ProcessedCacheSize = 1000 // Increased from 250 - ReconnectInterval = 7 * time.Second - MappingCacheSize = 1000 - MappingCleanupInterval = 5 * time.Minute - MappingMaxAge = 1 * time.Hour - OutboundMatchWindow = 60 * time.Second + ProcessedCacheSize = 1000 // Increased from 250 + ReconnectInterval = 7 * time.Second + MappingCacheSize = 1000 + MappingCleanupInterval = 5 * time.Minute + MappingMaxAge = 1 * time.Hour + OutboundMatchWindow = 60 * time.Second + PingIdleThreshold = 60 * time.Second + StaleRejoinThreshold = 90 * time.Second + StaleReconnectThreshold = 3 * time.Minute + RejoinCooldown = 30 * time.Second ) type Client struct { @@ -33,9 +37,10 @@ type Client struct { connected bool mu sync.RWMutex - lastMessage time.Time - stopCh chan struct{} - wg sync.WaitGroup + lastMessage time.Time + lastJoinAttempt time.Time + stopCh chan struct{} + wg sync.WaitGroup processedMu sync.Mutex processedMessageIDs []int @@ -50,9 +55,9 @@ type Client struct { recentOutboundIter func() []map[string]interface{} mapDiscordSneed func(int, int, string) - bridgeUserID int - bridgeUsername string - baseLoopsStarted bool + bridgeUserID int + bridgeUsername string + baseLoopsStarted bool } func NewClient(roomID int, cookieSvc *cookie.CookieRefreshService) *Client { @@ -107,7 +112,7 @@ func (c *Client) Connect() error { c.wg.Add(1) go c.readLoop() - c.Send(fmt.Sprintf("/join %d", c.roomID)) + c.joinRoom() log.Printf("✅ Successfully connected to Sneedchat room %d", c.roomID) if c.OnConnect != nil { c.OnConnect() @@ -115,8 +120,14 @@ func (c *Client) Connect() error { return nil } -func (c *Client) joinRoom() { - c.Send(fmt.Sprintf("/join %d", c.roomID)) +func (c *Client) joinRoom() bool { + sent := c.Send(fmt.Sprintf("/join %d", c.roomID)) + if sent { + c.mu.Lock() + c.lastJoinAttempt = time.Now() + c.mu.Unlock() + } + return sent } func (c *Client) readLoop() { @@ -148,7 +159,7 @@ func (c *Client) readLoop() { func (c *Client) heartbeatLoop() { defer c.wg.Done() - t := time.NewTicker(30 * time.Second) + t := time.NewTicker(15 * time.Second) defer t.Stop() for { select { @@ -157,15 +168,46 @@ func (c *Client) heartbeatLoop() { connected := c.connected conn := c.conn c.mu.RUnlock() - if connected && time.Since(c.lastMessage) > 60*time.Second && conn != nil { + if !connected || conn == nil { + continue + } + + silence := time.Since(c.lastMessage) + if silence > PingIdleThreshold { _ = conn.WriteMessage(websocket.TextMessage, []byte("/ping")) } + c.handleStaleState(silence) case <-c.stopCh: return } } } +func (c *Client) handleStaleState(silence time.Duration) { + if silence < StaleRejoinThreshold { + return + } + + if silence >= StaleReconnectThreshold { + log.Printf("⚠️ No Sneedchat messages for %s; recycling websocket", silence.Round(time.Second)) + c.handleDisconnect() + return + } + + c.mu.RLock() + lastJoin := c.lastJoinAttempt + c.mu.RUnlock() + if time.Since(lastJoin) < RejoinCooldown { + return + } + + if c.joinRoom() { + log.Printf("⚠️ Sneedchat feed silent for %s, reasserted /join %d", silence.Round(time.Second), c.roomID) + } else { + log.Printf("⚠️ Sneedchat feed silent for %s but websocket not writable; waiting", silence.Round(time.Second)) + } +} + func (c *Client) cleanupLoop() { defer c.wg.Done() t := time.NewTicker(MappingCleanupInterval) @@ -199,6 +241,13 @@ func (c *Client) Send(s string) bool { } func (c *Client) handleDisconnect() { + c.mu.RLock() + alreadyDisconnected := !c.connected + c.mu.RUnlock() + if alreadyDisconnected { + return + } + select { case <-c.stopCh: return @@ -231,10 +280,10 @@ func (c *Client) handleDisconnect() { case <-time.After(delay): attempt++ log.Printf("🔄 Reconnection attempt #%d...", attempt) - + if err := c.Connect(); err != nil { log.Printf("⚠️ Reconnect attempt #%d failed: %v", attempt, err) - + // Exponential backoff delay *= 2 if delay > maxDelay { @@ -398,14 +447,14 @@ func (c *Client) isProcessed(id int) bool { func (c *Client) addToProcessed(id int) { c.processedMu.Lock() defer c.processedMu.Unlock() - + c.processedMessageIDs = append(c.processedMessageIDs, id) - + // Hard cap: keep only the most recent 1000 messages (FIFO) if len(c.processedMessageIDs) > ProcessedCacheSize { excess := len(c.processedMessageIDs) - ProcessedCacheSize c.processedMessageIDs = c.processedMessageIDs[excess:] - + // Log when significant eviction happens if excess > 50 { log.Printf("⚠️ Processed message cache full, evicted %d old entries", excess) @@ -438,4 +487,4 @@ func ReplaceBridgeMention(content, bridgeUsername, pingID string) string { } pat := regexp.MustCompile(fmt.Sprintf(`(?i)@%s(?:\W|$)`, regexp.QuoteMeta(bridgeUsername))) return pat.ReplaceAllString(content, fmt.Sprintf("<@%s>", pingID)) -} \ No newline at end of file +}