Skip to main content

Crate reliakit_backoff

Crate reliakit_backoff 

Source
Expand description

Clock-agnostic retry backoff policies.

reliakit-backoff computes how long to wait between retries. It does not sleep, spawn tasks, or touch the clock — you decide when to call it and how to wait. That makes it usable from sync code, any async runtime, and no_std / embedded contexts, with deterministic, exact-byte-style tests.

The core type is Backoff: a small, Copy policy describing a base delay, a growth strategy (constant, linear, or exponential), an optional maximum delay, and an optional retry limit. Backoff::delay maps a zero-based attempt number to the delay to wait before that retry, or None once the retry limit is reached.

Randomized jitter is provided as explicit, pure functions (full_jitter, equal_jitter, decorrelated_jitter) that take a caller-supplied random value, so the crate stays dependency-free and its output stays deterministic in tests.

§Example

use core::time::Duration;
use reliakit_backoff::Backoff;

// 100ms base, double each attempt, capped at 2s, give up after 8 retries.
let policy = Backoff::exponential(Duration::from_millis(100), 2)
    .with_max_delay(Duration::from_secs(2))
    .with_max_retries(8);

assert_eq!(policy.delay(0), Some(Duration::from_millis(100)));
assert_eq!(policy.delay(1), Some(Duration::from_millis(200)));
assert_eq!(policy.delay(4), Some(Duration::from_millis(1600)));
assert_eq!(policy.delay(5), Some(Duration::from_secs(2))); // 3200ms capped to 2s
assert_eq!(policy.delay(8), None); // retry limit reached

// Drive your own retry loop (attempts 0..8 here):
for delay in policy.delays() {
    // sleep(delay); try_again()?; ...
    let _ = delay;
}

Structs§

Backoff
A retry backoff policy.
Delays
Iterator over the successive delays of a Backoff policy.

Functions§

decorrelated_jitter
Decorrelated jitter: returns a delay uniformly in base ..= prev * 3, capped at cap.
equal_jitter
Equal jitter: keeps half of delay fixed and randomizes the other half, returning a delay in delay/2 ..= delay.
full_jitter
Full jitter: returns a uniformly random delay in 0 ..= delay.