Skip to content

Retries for EventSource #367

Open
Open
@huntc

Description

@huntc

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions