relentless
Retry and polling for Rust — with composable strategies, policy reuse, and
first-class support for polling workflows where Ok(_) doesn't always mean
"done."
Most retry libraries handle the simple case well: call a function, retry on
error, back off. relentless handles that too, but it also handles the cases
those libraries make awkward:
- Polling, where
Ok("pending")means "keep going" and you need.until(predicate::ok(...))rather than just retrying errors. - Policy reuse, where a single
RetryPolicycaptures your retry rules and gets shared across multiple call sites — no duplicated builder chains. - Strategy composition, where
wait::fixed(50ms) + wait::exponential(100ms)andstop::attempts(5) | stop::elapsed(2s)express complex behavior in one line. - Hooks and stats, where you observe the retry lifecycle (logging, metrics) without restructuring your retry logic.
All of this works in sync and async code, across std, no_std, and wasm
targets.
Inspired by Python's tenacity (composable
strategy algebra) and Rust's backon
(ergonomic retry builders).
Install
Feature flags
| Flag | Purpose |
|---|---|
std (default) |
std::thread::sleep fallback, Instant elapsed clock, std::error::Error on RetryError |
alloc |
Boxed policies, closure elapsed clocks, multiple hooks per point |
tokio-sleep |
sleep::tokio() async sleep adapter |
embassy-sleep |
sleep::embassy() async sleep adapter |
gloo-timers-sleep |
sleep::gloo() async sleep adapter (wasm32) |
futures-timer-sleep |
sleep::futures_timer() async sleep adapter |
Async retry does not require alloc.
Examples
For full docs, see https://docs.rs/relentless. Behavior spec:
docs/SPEC.md. Runnable examples live in
examples/.
Sync examples omit .sleep(...) because std builds fall back to
std::thread::sleep automatically. Without std, pass an explicit sleeper
before .call().
1) Retry with defaults
The .retry() extension trait is the fastest way to add retries. Defaults: 3
attempts, exponential backoff from 100 ms, retry on any Err.
use RetryExt;
let results = fetch_job_output.retry.call;
2) Customized retry
The retry free function is equivalent to the extension trait, with the added
ability to capture retry loop state. Both the free function and extension trait
give full control over which errors to retry, how long to wait, and when to
stop.
use Duration;
use ;
let body = retry
.when
.wait
.stop
.timeout
.call;
3) Reuse a policy across call sites
RetryPolicy captures retry rules once. Compose wait strategies with + and
stop strategies with | or &.
use Duration;
use ;
let policy = new
.wait
.stop;
// Same policy, different operations.
let health = policy.retry.call;
let invoice = policy.retry.call;
4) Poll for a condition
Use .until(predicate) to keep retrying until a success condition is met.
Unlike .when(), which retries on matching outcomes, .until() retries on
everything except the matching outcome.
use ;
let result = new
.until
.retry
.call;
To also retry selected errors during polling, use predicate::result:
use ;
// Retry until Done or Fatal; keep going on Pending or Retryable.
let result = new
.until
.retry
.call;
5) Async retry
Pass an async sleep adapter — here via the tokio-sleep feature.
use retry_async;
async
async
6) Hooks & stats
use retry;
let = retry
.before_attempt
.after_attempt
.with_stats
.call;
println!;
7) Error handling
use ;
match retry.call
API surface at a glance
| Area | Items |
|---|---|
| Entry points | retry, retry_async (free functions); RetryExt, AsyncRetryExt (extension traits) |
| Policy | RetryPolicy<S, W, P> with .retry(), .retry_async() |
| Stop strategies | stop::attempts, stop::elapsed, stop::never |
| Wait strategies | wait::fixed, wait::linear, wait::exponential, wait::decorrelated_jitter |
| Predicates | predicate::any_error, predicate::error, predicate::ok, predicate::result |
| Execution builders | SyncRetryBuilder / AsyncRetryBuilder with hooks, stats, timeout |
| Terminal types | RetryError<T, E> (Exhausted, Rejected), RetryResult<T, E>, RetryStats, StopReason |
Builder methods follow the order: when/until -> wait -> stop -> sleep -> hooks -> stats -> call.
MSRV
Minimum supported Rust version: 1.85.
Contributing
See CONTRIBUTING.md.
Release notes
For user-facing changes, see the changelog.
License
Licensed under either:
- MIT (LICENSE-MIT)
- Apache-2.0 (LICENSE-APACHE)