fix: reconnect should use both reconnect and retry_initial for reconnection#99
Conversation
…ailed connection mid-stream) and retry_initial for initial reconnection
|
@jakub-hess apologies for the delay in addressing this PR. The provided code snippet you included, quoted again below, is not a supported use case. If you look at the tail example in this project, you will see an example of the usage we support. The client is not intended to be consumed once an error has returned. Once an error is returned, the client is considered defunct and a new one would need to be instantiated. |
|
Yes, I understand that, I didn't mention the fact that the inside of the loop has been abbreviated. Even if I break on error my main point still stands, the reconnection is driven by retry_initial instead of reconnect. If reconnect is true and retry_initial is false and the server drops the connection (e.g. because of restart) I get an HTTP Stream Error instead of the client trying to reconnect based on its reconnection settings. |
You are completely right. My apologies for misunderstanding initially. I appreciate you making the fix for this. |
🤖 I have created a release *beep* *boop* --- ## [0.15.1](0.15.0...0.15.1) (2025-10-07) ### Bug Fixes * Bump MSRV to 1.82.0 ([#95](#95)) ([ddf8753](ddf8753)) * Reconnect should use both reconnect and retry_initial for reconnection ([#99](#99)) ([9a91e67](9a91e67)) --- This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please). Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
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