Crates.io | mulligan |
lib.rs | mulligan |
version | 0.2.0 |
source | src |
created_at | 2024-10-27 17:11:18.832728 |
updated_at | 2024-10-31 01:27:58.323759 |
description | A flexible retry library for Rust async operations with configurable backoff strategies and jitter. |
homepage | https://github.com/theelderbeever/mulligan |
repository | https://github.com/theelderbeever/mulligan |
max_upload_size | |
id | 1424791 |
size | 40,068 |
A flexible retry library for Rust async operations with configurable backoff strategies and jitter.
mulligan
provides a fluent API for retrying async operations with customizable retry policies, backoff strategies, and jitter. It supports both tokio
and async-std
runtimes.
tokio
(via tokio
feature)async-std
(via async-std
feature)use std::time::Duration;
async fn fallible_operation(msg: &str) -> std::io::Result<()> {
// Your potentially failing operation here
Err(std::io::Error::other(msg))
}
#[tokio::main]
async fn main() {
let result = mulligan::until_ok()
.stop_after(5) // Try up to 5 times
.max_delay(Duration::from_secs(3)) // Cap maximum delay at 3 seconds
.exponential(Duration::from_secs(1)) // Use exponential backoff
.full_jitter() // Add randomized jitter
.retry(|| async {
fallible_operation("connection failed").await
})
.await;
}
Alternatively, you may provide a custom stopping condition. mulligan::until_ok()
is equivalent to the custom stopping condition shown below.
#[tokio::main]
async fn main() {
let result = mulligan::until(|res| res.is_ok())
.stop_after(5) // Try up to 5 times
.max_delay(Duration::from_secs(3)) // Cap maximum delay at 3 seconds
.exponential(Duration::from_secs(1)) // Use exponential backoff
.full_jitter() // Add randomized jitter
.on_retry(|prev, attempts| { // Run before each retry.
println!("In the {}-th attempt, the returned result is {:?}.", attempts, prev);
println!("Start next attempt");
})
.retry(|| async {
fallible_operation("connection failed").await
})
.await;
}
Add this to your Cargo.toml
:
[dependencies]
mulligan = { version = "0.1", features = ["tokio"] } # or ["async-std"]