[FEATURE] Implement reconnect logic and exponential backoff in Subscriber#11
Merged
Conversation
42c5b67 to
d03ed36
Compare
mesmerze
approved these changes
May 11, 2026
Comment on lines
+5
to
+6
| INITIAL_BACKOFF = 0.5 | ||
| MAX_BACKOFF = 30.0 |
Contributor
There was a problem hiding this comment.
INITIAL_RECONNECT_BACKOFF
MAX_RECONNECT_BACKOFF
🙏
ihor-tokaryk
approved these changes
May 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Make
RedisStream::Subscriber.listensurvive Redis restarts. Previously the listener crashed on any connection drop; now it logs the disconnect, retries with exponential backoff usingPINGto detect recovery, and resumes consumption. If Redis comes back without persistence (or the stream/group was wiped),NOGROUPis detected and the consumer groups are recreated automatically.What changed
lib/redis_stream/subscriber.rb:xreadgrouploop with rescues forRedis::BaseConnectionErrorandRedis::CommandError.wait_for_reconnect, which probesPINGwith exponential backoff (0.5s → 30s cap, doubling) until Redis is reachable, then resumes the consume loop.NOGROUP: callensure_groupsto recreate consumer groups on all streams, then resume.CommandErrors still propagate (e.g.WRONGTYPE).ensure_groups(streams, group), called both on startup and afterNOGROUP.NOGROUPrecovery). Logs go to stderr viawarnwith a[redis_stream]prefix.PINGis used (notxreadgroup) for recovery detection becausexreadgroupblocks waiting for messages — without a probe that returns immediately, we couldn't distinguish "Redis is back but no traffic" from "Redis still down."Behavior
WRONGTYPEor other command errorsBackoff caps at 30s. Downtime is measured with
Process::CLOCK_MONOTONIC(immune to NTP jumps) and reported in the reconnect log:reconnected to redis after 4 attempt(s); downtime 3.50s.Sample output