Skip to content

Commit

Permalink
signalmeow/websocket: don't disconnect on first ping timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Jan 7, 2025
1 parent 7ed141b commit 44f363f
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions pkg/signalmeow/web/signalwebsocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ func (s *SignalWebsocket) connectLoop(
log.Warn().Dur("backoff", backoff).Msg("Failed to connect, waiting to retry...")
time.Sleep(backoff)
backoff += backoffIncrement
} else if !isFirstConnect {
time.Sleep(1 * time.Second)
} else if !isFirstConnect && s.basicAuth != nil {
time.Sleep(initialBackoff)
}
if ctx.Err() != nil {
log.Info().Msg("ctx done, stopping connection loop")
Expand Down Expand Up @@ -300,18 +300,28 @@ func (s *SignalWebsocket) connectLoop(
ticker := time.NewTicker(30 * time.Second)
defer ticker.Stop()

pingTimeoutCount := 0
for {
select {
case <-ticker.C:
pingCtx, cancel := context.WithTimeout(loopCtx, 20*time.Second)
err := ws.Ping(pingCtx)
cancel()
if err != nil {
pingTimeoutCount++
log.Err(err).Msg("Error pinging")
loopCancel(err)
return
if pingTimeoutCount >= 5 {
log.Warn().Msg("Ping timeout count exceeded, closing websocket")
err = ws.Close(websocket.StatusNormalClosure, "Ping timeout")
if err != nil {
log.Err(err).Msg("Error closing websocket after ping timeout")
}
return
}
} else {
pingTimeoutCount = 0
log.Trace().Msg("Sent keepalive")
}
log.Trace().Msg("Sent keepalive")
case <-loopCtx.Done():
return
}
Expand Down

0 comments on commit 44f363f

Please sign in to comment.