Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retries for EventSource #367

Open
huntc opened this issue Aug 17, 2023 · 1 comment
Open

Retries for EventSource #367

huntc opened this issue Aug 17, 2023 · 1 comment

Comments

@huntc
Copy link
Contributor

huntc commented Aug 17, 2023

Summary

Extend the API of EventSource to specify what to do in the event of a ConnectionError.

Motivation

The primary advantage of Server Sent Events (SSE) is that the browser manages connectivity. While it mostly does this well, it turns out that a "503 - Service Unavailable" causes a ConnectionError, at least in Chrome. Other browsers may exhibit other behavior. A 503 is often a temporary condition so I have ended up coding up a retry with, for example, a delay of at least one second, and then a random 2, 3, 4, or 5 more seconds. Having the EventSource provide this as a built-in feature would cater for this common scenario and save the user a reasonable amount of code, which is compounded by dealing ConnectionError appearing for all message type subscriptions.

Detailed Explanation

I'm thinking that a new constructor could be provided:

pub fn with_connection_strategy(url: &str, connection_strategy: ConnectionStrategy) -> Result<Self, JsError>

where ConnectionStrategy could be:

pub enum ConnectionStrategy {
    /// Retry with exponential backing off between a min and max duration
    RetryWithBackoff(Duration, Duration),
    /// Existing behavior
    None,
}

Drawbacks, Rationale, and Alternatives

An implication would be an additional dependency on gloo-timers.

Unresolved Questions

Nothing at this stage.

@huntc
Copy link
Contributor Author

huntc commented Nov 11, 2023

I’d like to provide a PR here if there’s interest in it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant