A lightweight, zero-dependency Rust library for retrying fallible operations with flexible delay and backoff strategies.
- Zero External Dependencies: Fast compile times and zero bloat.
- Builder Pattern: Clean, chainable API for configuring retry behaviors.
- Exponential Backoff: Easily configure delay scaling factors for resilient network/IO operations.
- Zero-Cost Abstractions: Lightweight implementation designed for maximum performance.
- 100% Safe & Tested: Fully covered with unit tests and doc tests.
Add quick_retry to your Cargo.toml:
cargo add quick_retry[dependencies]
quick_retry = "0.1.0"Retry an operation up to 3 times with a default 100ms fixed delay:
use quick_retry::Retry;
use std::time::Duration;
fn main() {
let result = Retry::new()
.max_attempts(3)
.delay(Duration::from_millis(100))
.run(|| {
// Your fallible operation (I/O, network request, database query)
fetch_data()
});
match result {
Ok(data) => println!("Success: {}", data),
Err(err) => eprintln!("Failed after retries: {}", err),
}
}
fn fetch_data() -> Result<&'static str, &'static str> {
// Simulating operation
Ok("Data fetched successfully!")
}increase delay exponential after each failed attempt (e.g 100ms --> 200ms --> 400ms):
use quick_retry::Retry;
use std::time::Duration;
fn main() {
let result = Retry::new()
.max_attempts(5)
.delay(Duration::from_millis(100))
.exponential_backoff(2.0) // Doubles the delay each time
.run(|| {
connect_to_server()
});
if let Ok(connection) = result {
println!("Connected: {:?}", connection);
}
}
fn connect_to_server() -> Result<&'static str, &'static str> {
Err("Connection timed out")
}| Method | Dercription | Default |
|---|---|---|
Retry::new() |
Creates Retry build instance. |
max_attempts: 3, delay: 100ms , Fixed |
.max_attempts(u32) |
Sets the maximum number of attempts before failing. | 3 |
.delay(Duration) |
Sets the initial delay duration between | 100ms |
exponential_backoff(f64) |
Enables exponential backoff with th given multiplier factor. | Disabled(Fixed) |
.run(F) |
Executes the operation closure FnMut() -> Result<T,E>. |
N/A |
This project Licensed under the MIT License-see the LICENSE file for details.
if you find this crate useful, please consider given it a star on GitHub