gloo icon indicating copy to clipboard operation
gloo copied to clipboard

Retries for EventSource

Open huntc opened this issue 1 year ago • 1 comments

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 avatar Aug 17 '23 06:08 huntc