Fix: Silent failure on reconnect, self healing via /join when chat is silent after an outage
This commit is contained in:
@@ -22,6 +22,10 @@ const (
|
||||
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 {
|
||||
@@ -34,6 +38,7 @@ type Client struct {
|
||||
mu sync.RWMutex
|
||||
|
||||
lastMessage time.Time
|
||||
lastJoinAttempt time.Time
|
||||
stopCh chan struct{}
|
||||
wg sync.WaitGroup
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user