Fix: Silent failure on reconnect, self healing via /join when chat is silent after an outage
All checks were successful
Build & Release / build-latest (push) Has been skipped
Build & Release / version-release (push) Successful in 10m26s

This commit is contained in:
Salastil
2025-11-18 20:21:58 -05:00
parent f954771c0c
commit 60ed8c3ca0

View File

@@ -16,12 +16,16 @@ import (
) )
const ( const (
ProcessedCacheSize = 1000 // Increased from 250 ProcessedCacheSize = 1000 // Increased from 250
ReconnectInterval = 7 * time.Second ReconnectInterval = 7 * time.Second
MappingCacheSize = 1000 MappingCacheSize = 1000
MappingCleanupInterval = 5 * time.Minute MappingCleanupInterval = 5 * time.Minute
MappingMaxAge = 1 * time.Hour MappingMaxAge = 1 * time.Hour
OutboundMatchWindow = 60 * time.Second OutboundMatchWindow = 60 * time.Second
PingIdleThreshold = 60 * time.Second
StaleRejoinThreshold = 90 * time.Second
StaleReconnectThreshold = 3 * time.Minute
RejoinCooldown = 30 * time.Second
) )
type Client struct { type Client struct {
@@ -33,9 +37,10 @@ type Client struct {
connected bool connected bool
mu sync.RWMutex mu sync.RWMutex
lastMessage time.Time lastMessage time.Time
stopCh chan struct{} lastJoinAttempt time.Time
wg sync.WaitGroup stopCh chan struct{}
wg sync.WaitGroup
processedMu sync.Mutex processedMu sync.Mutex
processedMessageIDs []int processedMessageIDs []int
@@ -50,9 +55,9 @@ type Client struct {
recentOutboundIter func() []map[string]interface{} recentOutboundIter func() []map[string]interface{}
mapDiscordSneed func(int, int, string) mapDiscordSneed func(int, int, string)
bridgeUserID int bridgeUserID int
bridgeUsername string bridgeUsername string
baseLoopsStarted bool baseLoopsStarted bool
} }
func NewClient(roomID int, cookieSvc *cookie.CookieRefreshService) *Client { func NewClient(roomID int, cookieSvc *cookie.CookieRefreshService) *Client {
@@ -107,7 +112,7 @@ func (c *Client) Connect() error {
c.wg.Add(1) c.wg.Add(1)
go c.readLoop() go c.readLoop()
c.Send(fmt.Sprintf("/join %d", c.roomID)) c.joinRoom()
log.Printf("✅ Successfully connected to Sneedchat room %d", c.roomID) log.Printf("✅ Successfully connected to Sneedchat room %d", c.roomID)
if c.OnConnect != nil { if c.OnConnect != nil {
c.OnConnect() c.OnConnect()
@@ -115,8 +120,14 @@ func (c *Client) Connect() error {
return nil return nil
} }
func (c *Client) joinRoom() { func (c *Client) joinRoom() bool {
c.Send(fmt.Sprintf("/join %d", c.roomID)) 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() { func (c *Client) readLoop() {
@@ -148,7 +159,7 @@ func (c *Client) readLoop() {
func (c *Client) heartbeatLoop() { func (c *Client) heartbeatLoop() {
defer c.wg.Done() defer c.wg.Done()
t := time.NewTicker(30 * time.Second) t := time.NewTicker(15 * time.Second)
defer t.Stop() defer t.Stop()
for { for {
select { select {
@@ -157,15 +168,46 @@ func (c *Client) heartbeatLoop() {
connected := c.connected connected := c.connected
conn := c.conn conn := c.conn
c.mu.RUnlock() 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")) _ = conn.WriteMessage(websocket.TextMessage, []byte("/ping"))
} }
c.handleStaleState(silence)
case <-c.stopCh: case <-c.stopCh:
return 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() { func (c *Client) cleanupLoop() {
defer c.wg.Done() defer c.wg.Done()
t := time.NewTicker(MappingCleanupInterval) t := time.NewTicker(MappingCleanupInterval)
@@ -199,6 +241,13 @@ func (c *Client) Send(s string) bool {
} }
func (c *Client) handleDisconnect() { func (c *Client) handleDisconnect() {
c.mu.RLock()
alreadyDisconnected := !c.connected
c.mu.RUnlock()
if alreadyDisconnected {
return
}
select { select {
case <-c.stopCh: case <-c.stopCh:
return return
@@ -231,10 +280,10 @@ func (c *Client) handleDisconnect() {
case <-time.After(delay): case <-time.After(delay):
attempt++ attempt++
log.Printf("🔄 Reconnection attempt #%d...", attempt) log.Printf("🔄 Reconnection attempt #%d...", attempt)
if err := c.Connect(); err != nil { if err := c.Connect(); err != nil {
log.Printf("⚠️ Reconnect attempt #%d failed: %v", attempt, err) log.Printf("⚠️ Reconnect attempt #%d failed: %v", attempt, err)
// Exponential backoff // Exponential backoff
delay *= 2 delay *= 2
if delay > maxDelay { if delay > maxDelay {
@@ -398,14 +447,14 @@ func (c *Client) isProcessed(id int) bool {
func (c *Client) addToProcessed(id int) { func (c *Client) addToProcessed(id int) {
c.processedMu.Lock() c.processedMu.Lock()
defer c.processedMu.Unlock() defer c.processedMu.Unlock()
c.processedMessageIDs = append(c.processedMessageIDs, id) c.processedMessageIDs = append(c.processedMessageIDs, id)
// Hard cap: keep only the most recent 1000 messages (FIFO) // Hard cap: keep only the most recent 1000 messages (FIFO)
if len(c.processedMessageIDs) > ProcessedCacheSize { if len(c.processedMessageIDs) > ProcessedCacheSize {
excess := len(c.processedMessageIDs) - ProcessedCacheSize excess := len(c.processedMessageIDs) - ProcessedCacheSize
c.processedMessageIDs = c.processedMessageIDs[excess:] c.processedMessageIDs = c.processedMessageIDs[excess:]
// Log when significant eviction happens // Log when significant eviction happens
if excess > 50 { if excess > 50 {
log.Printf("⚠️ Processed message cache full, evicted %d old entries", excess) 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))) pat := regexp.MustCompile(fmt.Sprintf(`(?i)@%s(?:\W|$)`, regexp.QuoteMeta(bridgeUsername)))
return pat.ReplaceAllString(content, fmt.Sprintf("<@%s>", pingID)) return pat.ReplaceAllString(content, fmt.Sprintf("<@%s>", pingID))
} }