pub struct ReconnectPolicy {
pub max_attempts: u32,
pub initial_backoff: Duration,
pub max_backoff: Duration,
pub multiplier: f64,
pub jitter: f64,
}Expand description
Reconnection policy for a WebSocket feed.
Controls exponential-backoff reconnect behaviour. Build with
ReconnectPolicy::new or use Default for sensible defaults.
Fields§
§max_attempts: u32Maximum number of reconnect attempts before giving up.
initial_backoff: DurationInitial backoff delay for the first reconnect attempt.
max_backoff: DurationMaximum backoff delay (cap for exponential growth).
multiplier: f64Multiplier applied to the backoff on each successive attempt (must be >= 1.0).
jitter: f64Jitter ratio in [0.0, 1.0]: the computed backoff is offset by up to
±ratio × backoff using a deterministic per-attempt hash. 0.0 = no jitter.
Implementations§
Source§impl ReconnectPolicy
impl ReconnectPolicy
Sourcepub fn new(
max_attempts: u32,
initial_backoff: Duration,
max_backoff: Duration,
multiplier: f64,
) -> Result<Self, StreamError>
pub fn new( max_attempts: u32, initial_backoff: Duration, max_backoff: Duration, multiplier: f64, ) -> Result<Self, StreamError>
Build a reconnect policy with explicit parameters.
§Errors
Returns StreamError::ConfigError if multiplier < 1.0 (which would
cause backoff to shrink over time) or if max_attempts == 0.
Sourcepub fn with_max_attempts(self, max_attempts: u32) -> Result<Self, StreamError>
pub fn with_max_attempts(self, max_attempts: u32) -> Result<Self, StreamError>
Set the maximum number of reconnect attempts.
§Errors
Returns StreamError::ConfigError if max_attempts is zero.
Sourcepub fn with_multiplier(self, multiplier: f64) -> Result<Self, StreamError>
pub fn with_multiplier(self, multiplier: f64) -> Result<Self, StreamError>
Set the exponential backoff multiplier.
§Errors
Returns StreamError::ConfigError if multiplier < 1.0 (which would
cause the backoff to shrink on each attempt).
Sourcepub fn with_initial_backoff(self, duration: Duration) -> Self
pub fn with_initial_backoff(self, duration: Duration) -> Self
Set the initial backoff duration for the first reconnect attempt.
Sourcepub fn with_max_backoff(self, duration: Duration) -> Self
pub fn with_max_backoff(self, duration: Duration) -> Self
Set the maximum backoff duration (cap for exponential growth).
Sourcepub fn with_jitter(self, ratio: f64) -> Result<Self, StreamError>
pub fn with_jitter(self, ratio: f64) -> Result<Self, StreamError>
Apply deterministic per-attempt jitter to the computed backoff.
ratio must be in [0.0, 1.0]. The effective backoff for attempt N
will be spread uniformly over [backoff*(1-ratio), backoff*(1+ratio)]
using a hash of the attempt index — no rand dependency needed.
§Errors
Returns StreamError::ConfigError if ratio is outside [0.0, 1.0].
Sourcepub fn total_max_delay(&self) -> Duration
pub fn total_max_delay(&self) -> Duration
Sum of all backoff delays across every reconnect attempt.
Useful for estimating the worst-case time before a client gives up.
The result is capped at max_backoff * max_attempts to avoid overflow.
Sourcepub fn max_attempts(&self) -> u32
pub fn max_attempts(&self) -> u32
Maximum number of reconnect attempts before the client gives up.
Sourcepub fn total_attempts_remaining(&self, current_attempt: u32) -> u32
pub fn total_attempts_remaining(&self, current_attempt: u32) -> u32
Number of attempts remaining starting from current_attempt (0-indexed).
Returns 0 if current_attempt >= max_attempts.
Sourcepub fn delay_for_next(&self, current_attempt: u32) -> Duration
pub fn delay_for_next(&self, current_attempt: u32) -> Duration
Backoff delay that will be applied after current_attempt completes.
Equivalent to backoff_for_attempt(current_attempt + 1), capped at
max_backoff. Saturates rather than wrapping when current_attempt
is u32::MAX.
Sourcepub fn is_exhausted(&self, attempts: u32) -> bool
pub fn is_exhausted(&self, attempts: u32) -> bool
Returns true if attempts has reached or exceeded max_attempts.
After this returns true the reconnect loop should give up rather than
scheduling another attempt.
Sourcepub fn backoff_for_attempt(&self, attempt: u32) -> Duration
pub fn backoff_for_attempt(&self, attempt: u32) -> Duration
Backoff duration for attempt N (0-indexed).
Trait Implementations§
Source§impl Clone for ReconnectPolicy
impl Clone for ReconnectPolicy
Source§fn clone(&self) -> ReconnectPolicy
fn clone(&self) -> ReconnectPolicy
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more