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
|
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 {
|
||||||
@@ -34,6 +38,7 @@ type Client struct {
|
|||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
|
|
||||||
lastMessage time.Time
|
lastMessage time.Time
|
||||||
|
lastJoinAttempt time.Time
|
||||||
stopCh chan struct{}
|
stopCh chan struct{}
|
||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
|
|
||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user