pub struct Backoff {
pub initial: Duration,
pub multiplier: f64,
pub max: Duration,
pub jitter: Jitter,
}Expand description
Exponential-backoff configuration.
delay(attempt) returns min(initial × multiplier^attempt, max),
then applies the configured Jitter policy.
All fields are pub so you can construct + tune directly, or use
one of the preset constructors (Backoff::smtp_outbound,
Backoff::auth_lockout, etc.).
Fields§
§initial: DurationDelay for attempt = 0.
multiplier: f64Geometric growth factor between attempts. Common values: 2.0 (doubling) or 1.5 (gentler ramp).
max: DurationHard ceiling on delay — once initial × multiplier^attempt
would exceed max, return max.
jitter: JitterJitter policy applied to the computed delay.
Implementations§
Source§impl Backoff
impl Backoff
Sourcepub fn new(
initial: Duration,
multiplier: f64,
max: Duration,
jitter: Jitter,
) -> Self
pub fn new( initial: Duration, multiplier: f64, max: Duration, jitter: Jitter, ) -> Self
Construct with explicit values. Use the preset constructors below for common shapes.
Sourcepub fn smtp_outbound() -> Self
pub fn smtp_outbound() -> Self
Preset for SMTP outbound delivery retries: 60s initial, 2.5×
growth, 8-hour cap, Full jitter. Mirrors the legacy
mailrs-outbound-queue schedule shape.
Sourcepub fn auth_lockout() -> Self
pub fn auth_lockout() -> Self
Preset for failed-auth lockout: 30min initial, 2× growth, 24-hour cap, None jitter (lockouts are deterministic by design — you want every offender to see exactly the same penalty).
Sourcepub fn webhook() -> Self
pub fn webhook() -> Self
Preset for webhook delivery retries: 60s initial, 2× growth, 6-hour cap, Equal jitter. Smoother than Full jitter so subscriber endpoints see a less spiky retry pattern.
Sourcepub fn base_delay(&self, attempt: u32) -> Duration
pub fn base_delay(&self, attempt: u32) -> Duration
Base delay for attempt BEFORE jitter is applied. Useful when
you want to log “scheduled delay” without the random part.
Sourcepub fn delay(&self, attempt: u32, seed: u64) -> Duration
pub fn delay(&self, attempt: u32, seed: u64) -> Duration
Compute the delay for attempt, applying the configured jitter.
seed is a caller-supplied source of randomness — usually
derived from a fresh rand::random::<u64>() or
std::time::Instant::now().elapsed().as_nanos(). The crate
itself has no RNG dependency.
For Jitter::None the seed is ignored. For Equal/Full it
drives a deterministic mapping (same seed → same delay), so
tests can be reproducible.
Sourcepub fn should_give_up(attempt: u32, max_attempts: u32) -> bool
pub fn should_give_up(attempt: u32, max_attempts: u32) -> bool
Convenience: should this attempt give up?
Returns true if attempt >= max_attempts.