Skip to content

Commit

Permalink
Handle ws == nil, and ws reconnect loops
Browse files Browse the repository at this point in the history
  • Loading branch information
smweber committed Nov 23, 2023
1 parent e20d746 commit 8f3f50e
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions pkg/signalmeow/web/signalwebsocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ func (s *SignalWebsocket) connectLoop(
// kill everything (including the websocket) and build it all up again
backoff := backoffIncrement
retrying := false
errorCount := 0
for {
if retrying {
if backoff > maxBackoff {
Expand Down Expand Up @@ -286,8 +287,13 @@ func (s *SignalWebsocket) connectLoop(
select {
case <-loopCtx.Done():
zlog.Info().Msgf("received loopCtx done (%s)", s.name)
zlog.Debug().Msgf("loopCtx error: %v", loopCtx.Err())
zlog.Debug().Msgf("loopCtx cause: %v", context.Cause(loopCtx))
if context.Cause(loopCtx) != nil {
err := context.Cause(loopCtx)
if err != nil && err != context.Canceled {
zlog.Err(err).Msg("loopCtx error")
errorCount++
}
}
if context.Cause(loopCtx) != nil && context.Cause(loopCtx) == context.Canceled {
s.statusChannel <- SignalWebsocketConnectionStatus{
Event: SignalWebsocketConnectionEventCleanShutdown,
Expand All @@ -298,12 +304,6 @@ func (s *SignalWebsocket) connectLoop(
Err: err,
}
}
if context.Cause(loopCtx) != nil {
err := context.Cause(loopCtx)
if err != nil && err != context.Canceled {
zlog.Err(err).Msg("loopCtx error")
}
}
case <-ctx.Done():
zlog.Info().Msgf("received ctx done (%s)", s.name)
zlog.Debug().Msgf("ctx error: %v", ctx.Err())
Expand All @@ -329,6 +329,12 @@ func (s *SignalWebsocket) connectLoop(
}
loopCancel(nil)
zlog.Debug().Msg("Finished websocket cleanup")
if errorCount > 500 {
// Something is really wrong, we better panic.
// This is a last defense against a runaway error loop,
// like the WS continually closing and reconnecting
zlog.Fatal().Msgf("Too many errors (%d), panicking (%s)", errorCount, s.name)
}
}
}

Expand Down Expand Up @@ -516,7 +522,9 @@ func OpenWebsocket(ctx context.Context, path string) (*websocket.Conn, *http.Res
}
urlStr := "wss://" + UrlHost + path
ws, resp, err := websocket.Dial(ctx, urlStr, opt)
ws.SetReadLimit(1 << 20) // Increase read limit to 1MB from default of 32KB
if ws != nil {
ws.SetReadLimit(1 << 20) // Increase read limit to 1MB from default of 32KB
}
return ws, resp, err
}

Expand Down

0 comments on commit 8f3f50e

Please sign in to comment.