fix: reset the streaming reconnect budget after a healthy session - #49
Merged
Merged
Conversation
MaxReconnectAttempts is documented as "the number of consecutive reconnect attempts before the stream terminates". The run loop incremented one counter and never reset it, so it was really a lifetime disconnect count: a stream that recovered fully, ran, and dropped again later spent budget it had already earned back, and eventually terminated a working subscription. Reproduced on main before the fix. With MaxReconnectAttempts=1 and three healthy sessions staged, only two confirmed subscriptions were established before "stream reconnect failed after 1 attempt(s)". connectAndRead now reports whether the session reached healthy progress, and the run loop resets both the attempt count and the backoff when it did. Healthy progress is deliberately not the TCP dial and not the handshake alone: the subscription must be confirmed AND at least one further server frame — an ActionCable ping or a channel message — must arrive on the same connection. Resetting on the dial would reset on a server that accepts and hangs up, which turns the cap into an unbounded rapid-flap loop. One frame past the confirmation proves a live session rather than a reachable socket, and it is a frame the server sends on its own, since ActionCable pings every few seconds. handleFrame now returns the frame kind alongside the fatal error so the accounting reads the frames already being parsed rather than parsing twice. A server-initiated "disconnect" notice is explicitly not healthy progress. Covered by tests: the budget is restored by a healthy session; a confirm-then-immediate-drop server never restores it and is still capped; a rejected subscription still terminates without a single retry; and Close() during backoff stops the loop and leaves no goroutine behind. Closes #34 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015ao5paex73xXvuM424Libo
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars. ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Advanced Run ID: Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Merged
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.
Closes #34
The defect
MaxReconnectAttemptsis documented as "the number of consecutive reconnect attempts before the stream terminates".run()incremented one counter at stream.go:351 and never reset it, so it was really a lifetime disconnect count. A stream that recovered fully, ran healthily, and dropped again later spent budget it had already earned back — and eventually terminated a subscription that was working.The comment on
connectAndReadeven claimed the opposite ("A successful subscription resets the caller's backoff counter"). Nothing implemented it.Verified still reproducing on
main(cd51e3a), matching the evidence in the issue:The healthy-progress boundary
The issue's own constraint: "Define a meaningful healthy-progress boundary rather than resetting on TCP dial and creating an infinite rapid-flap loop."
A session is healthy once the subscription has been confirmed AND at least one further server frame — an ActionCable ping or a channel message — has arrived on that same connection.
welcomereceivedwelcomearrives before the client even subscribes.confirm_subscriptionaloneA time-based threshold was rejected as the primary rule: it makes the test timing-dependent and it picks an arbitrary number that is wrong for any server whose legitimate session length sits near it.
The change
connectAndReadreturns(healthy bool, err error);run()resets both the attempt count and the backoff whenhealthyis true.handleFramereturns the frame kind alongside the fatal error, so the accounting reads frames already being parsed rather than parsing them twice.disconnectnotice is explicitly not healthy progress — the server is telling us to go away.MaxReconnectAttemptsandWithStreamMaxReconnectAttemptsnow document the reset semantics; the unlimited-retry opt-in (a negative value) is untouched.Red
Tests written first, run against unmodified
main:The three passing tests are the negative paths the issue asks for. They are green before and after by design — they exist to prove the fix does not buy the reset by breaking the cap, the fatal-rejection rule, or teardown.
The tests drive a real
httptestWebSocket server whose per-connection behaviour is scripted by connection index, so what is asserted is the number of subscriptions the server actually saw and the stream's terminating error — not an internal counter.Green
Acceptance criteria from the issue
TestStreamHealthySessionResetsReconnectBudget(red above)reconnect failedonce the server stops recovering) +TestStreamConfirmThenImmediateDropDoesNotResetBudgetTestStreamConfirmThenImmediateDropDoesNotResetBudget— at most 1 initial + 2 attemptsTestStreamRejectedSubscriptionStillTerminatesWithoutRetrying— exactly 1 subscribe, no retryTestStreamCloseDuringBackoffLeavesNoGoroutine, plus the existingTestStreamContextCancelTeardownandTestStreamCloseIsIdempotentStreamOptions.MaxReconnectAttempts,WithStreamMaxReconnectAttempts,connectAndRead; negative value untouchedgo test ./...and race testsFull suite
go test ./...ok github.com/OilpriceAPI/oilpriceapi-go 17.679s— 289 pass / 0 fail (285 baseline + 4 new)go test -race ./...ok github.com/OilpriceAPI/oilpriceapi-go 19.226sgo test -race -count=5 -run TestStream .ok github.com/OilpriceAPI/oilpriceapi-go 3.786s(flake check on the timing-sensitive stream tests)go vet ./...gofmt -l .Scope
handleFrameandconnectAndReadare unexported; no public API changes. No version bump, no tag, no release.🤖 Generated with Claude Code
https://claude.ai/code/session_015ao5paex73xXvuM424Libo