1 unstable release
| 0.1.0 | May 17, 2026 |
|---|
#2330 in Algorithms
8KB
50 lines
tool-retry-policy
Declarative retry policy primitive. Returns "how long to wait, then retry" or "give up". You run the call; this crate decides whether to retry and for how long.
- Exponential backoff:
base * 2^(attempt-1), capped atmax. - Optional uniform jitter (default ±25%) using a tiny PRNG.
- Per-attempt cap (
max_attempts).
Example
use tool_retry_policy::{Policy, Decision};
use std::time::Duration;
let p = Policy {
max_attempts: 4,
base: Duration::from_millis(100),
max: Duration::from_secs(10),
jitter: false,
};
let mut attempt = 0;
loop {
attempt += 1;
// run the call here; pretend it failed
match p.next(attempt) {
Decision::Retry(d) => { /* sleep d, continue */ break }
Decision::GiveUp => break,
}
}
tool-retry-policy
Declarative retry policy primitive. Tells you Retry(duration) or GiveUp; you run the actual call. Exponential backoff, deterministic jitter, per-policy attempt cap.
use tool_retry_policy::{Policy, Decision};
use std::time::Duration;
let p = Policy::default();
match p.next(2) {
Decision::Retry(d) => println!("sleep {d:?}"),
Decision::GiveUp => {},
}
Zero deps. MIT or Apache-2.0.