Expand description
Retry async operations with configurable backoff strategies.
This crate provides a single retry function backed by a Config
that controls how many attempts are made and the delay between them.
§Example
use std::time::Duration;
use retry::{retry, Config};
use retry::delay::{Exponential, Fixed};
// Exponential backoff, max 5 attempts, with jitter
let result = retry(
|| async { Ok::<_, &'static str>(42) },
Config::new(
Exponential::new(Duration::from_millis(100))
.with_max(Duration::from_secs(5))
.with_jitter(),
)
.with_attempts(5),
)
.await?;
// Fixed delay, 3 attempts, with on_retry callback
let result = retry(
|| async { Ok::<_, &'static str>(42) },
Config::new(Fixed::new(Duration::from_secs(1)))
.with_attempts(3)
.with_on_retry(|attempt, delay| {
eprintln!("retry #{attempt} in {delay:?}");
}),
)
.await?;Re-exports§
pub use error::Error;
Modules§
Structs§
Functions§
- retry
- Retries a fallible async operation with the given configuration.