fix: reconnect should use both reconnect and retry_initial for reconnection #99
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.
Current behaviour
In the case where the SSE stream is available on connect and breaks mid-connection (e.g. because of server restart or network failure) and when polled in loop as such:
The Client goes through following State transitions New -> Connecting -> Connected -> WaitingToReconnect -> New -> Connecting -> New -> Connecting -> New -> Connecting -> ....
It never waits for the delay_duration (this causes reconnects happening in about 1 ms rhythm), since it never goes to WaitingForReconnect again.
Cause
In client.rs on line 457
this is the retry flag, that is used further down on line 524, and it is always set to retry_initial, regardless of whether this is the initial connection, or reconnect of already established stream.
This error condition happens, when the server is still not ready to accept the connection, and therefore in my case this causes the stream to loop indefinitely (or break after first reconnect attempt depending on how i handle the errors)
Solution
Introduce a initial_connection flag, that is set to false once the Client reaches the Connected, and the in New state the reconnect or retry initial flag is used depending on the state of the initial_connection flag