From 9742da32a87b36bd96c79a37c7a4fdd12f579000 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Snorre=20Magnus=20Dav=C3=B8en?= Date: Tue, 19 Nov 2024 15:57:55 +0100 Subject: [PATCH] fix: Add check for backoff stop to stop websocket connects When backoff indicates we should stop, actually stop the reconnect logic. --- firehose/firehose.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/firehose/firehose.go b/firehose/firehose.go index 1b82beb..0fb6b8b 100644 --- a/firehose/firehose.go +++ b/firehose/firehose.go @@ -25,7 +25,7 @@ import ( // Subscribe to the firehose using the Firehose struct as a receiver func Subscribe(ctx context.Context, postChan chan interface{}, ticker *time.Ticker, seq int64) { - address := "wss://bsky.network/xrpc/com.atproto.sync.subscribeRepos" + address := "wss://bsky.network/xrpc/com.atproto.sync.subscribeRepo" headers := http.Header{} headers.Set("User-Agent", "NorSky: https://github.com/snorremd/norsky") @@ -37,9 +37,9 @@ func Subscribe(ctx context.Context, postChan chan interface{}, ticker *time.Tick dialer := websocket.DefaultDialer backoff := backoff.NewExponentialBackOff() - backoff.InitialInterval = 3 * time.Second + backoff.InitialInterval = 5 * time.Second backoff.MaxInterval = 30 * time.Second - backoff.Multiplier = 1.5 + backoff.Multiplier = 2 backoff.MaxElapsedTime = 120 * time.Second // Check if context is cancelled, if so exit the connection loop @@ -52,7 +52,16 @@ func Subscribe(ctx context.Context, postChan chan interface{}, ticker *time.Tick conn, _, err := dialer.Dial(address, nil) if err != nil { log.Errorf("Error connecting to firehose: %s", err) - time.Sleep(backoff.NextBackOff()) + + // Get the next backoff duration + duration := backoff.NextBackOff() + + if duration == backoff.Stop { + log.Warn("MaxElapsedTime reached. Stopping reconnect attempts.") + return // Exit the loop + } + + time.Sleep(duration) // Increase backoff by factor of 1.3, rounded to nearest whole number continue }