Fix eventsource reconnection: proper backoff and close() behavior#649
Open
jvosloo wants to merge 1 commit intobigskysoftware:masterfrom
Open
Fix eventsource reconnection: proper backoff and close() behavior#649jvosloo wants to merge 1 commit intobigskysoftware:masterfrom
jvosloo wants to merge 1 commit intobigskysoftware:masterfrom
Conversation
Fixes three bugs in the EventSource plugin reconnection logic: 1. XOR instead of exponentiation: 2 ^ retryCount uses JavaScript bitwise XOR, not exponentiation. This produces erratic delays (e.g. 2 ^ 2 = 0, giving 0ms delay). Fixed to Math.pow(2, n). 2. Backoff only triggers on CLOSED state: The error handler only reconnected when readyState == EventSource.CLOSED, but browsers keep readyState = CONNECTING on connection-refused errors and auto-retry every ~3 seconds -- bypassing the backoff entirely. Fixed by closing the EventSource on error to stop the browser native auto-reconnect, then managing retry with proper backoff. 3. close() does not stop reconnection: Calling close() in a user on error handler sets readyState to CLOSED, which immediately triggers the plugin own error handler to call open() -- making the problem worse. Fixed by tracking a closed flag that is set in close() and checked before scheduling reconnection. Fixes bigskysoftware#259
24b4720 to
d9b3226
Compare
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
Fixes three bugs in the EventSource plugin's reconnection logic that cause rapid-fire reconnection attempts when the server is unreachable, and prevent
.close()from working in error handlers.Bug 1: XOR instead of exponentiation
Bug 2: Backoff only triggers on
CLOSEDstateThe error handler only reconnected when
readyState == EventSource.CLOSED, but browsers keepreadyState = CONNECTINGon connection-refused errors and auto-retry every ~3 seconds — completely bypassing the backoff. Fixed by closing the EventSource on error to stop the browser's native auto-reconnect, then managing retry ourselves with proper exponential backoff.Bug 3:
.close()does not stop reconnection (#259)Calling
.close()in a user'son errorhandler setsreadyStatetoCLOSED, which immediately triggers the plugin's own error handler to callopen()— making the problem worse (as reported in #259). Fixed by:closedflag set by.close()and checked before scheduling reconnection.close()closedon.open()so reopening after explicit close still worksBackoff behavior after fix
Closes #259