pub struct SseRetryConfig {
pub max_retries: u32,
pub max_backoff_ms: u32,
pub min_sleep_ms: u32,
pub backoff_multiplier: f32,
pub jitter: bool,
}Expand description
Configuration for exponential backoff and jitter during stream reconnections.
Fields§
§max_retries: u32The maximum number of consecutive connection attempts before giving up.
max_backoff_ms: u32The absolute maximum wait time between connection attempts in milliseconds.
min_sleep_ms: u32The absolute minimum wait time between connection attempts in milliseconds.
backoff_multiplier: f32The multiplier applied to the delay after each failed attempt.
jitter: boolWhether to apply randomness (jitter) to the reconnect delay.
Implementations§
Source§impl SseRetryConfig
impl SseRetryConfig
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Creates a new retry configuration with sensible defaults.
The default configuration applies an exponential backoff multiplier of 2.0, caps the
maximum delay at 60,000 milliseconds (1 minute), caps the number of retries to 20, and
enables jitter to prevent thundering herd scenarios.
§Example
use sse_core::SseRetryConfig;
let config = SseRetryConfig::new();
assert_eq!(config.max_backoff_ms, 60_000);
assert!(config.jitter);Sourcepub const fn disabled() -> Self
pub const fn disabled() -> Self
Creates a retry configuration that disables all automatic retries.
Sourcepub fn calculate_backoff_with_factor(
&self,
reconnect_time_ms: u32,
attempt: u32,
jitter_factor: f32,
) -> Option<Duration>
pub fn calculate_backoff_with_factor( &self, reconnect_time_ms: u32, attempt: u32, jitter_factor: f32, ) -> Option<Duration>
Calculates the delay duration for the next reconnection attempt.
jitter_factor selects a point between the base delay and the computed
backoff; values outside 0.0..=1.0 (and non-finite ones) are treated as
1.0. It is ignored unless Self::jitter is set and the backoff has
actually grown past the base delay, since otherwise there is nothing to
pick between.
Returns None if the attempt count exceeds Self::max_retries.
§Panics
Panics if Self::min_sleep_ms is greater than Self::max_backoff_ms,
which would make the delay range empty. Both fields are public, so this is
reachable by mutating a config after construction; the constructors
(new(), disabled()) never produce such a
config. An exhausted attempt returns None before the check, so such a
config only panics while it still has retries left.
Sourcepub fn calculate_backoff(
&self,
reconnect_time_ms: u32,
attempt: u32,
) -> Option<Duration>
pub fn calculate_backoff( &self, reconnect_time_ms: u32, attempt: u32, ) -> Option<Duration>
Calculates the delay duration for the next reconnection attempt, drawing
the jitter factor from fastrand.
Returns None if the attempt count exceeds Self::max_retries.
§Panics
Panics under the same conditions as
calculate_backoff_with_factor().
Trait Implementations§
Source§impl Clone for SseRetryConfig
impl Clone for SseRetryConfig
Source§fn clone(&self) -> SseRetryConfig
fn clone(&self) -> SseRetryConfig
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more