A simple, flexible retry library for Rust that handles operations that may fail.
tryumph
provides mechanisms for executing operations with customizable retry strategies. It supports both synchronous and asynchronous execution models with various delay strategies for retries.
- Synchronous retries through the
sync
module - Asynchronous retries through the
unsync
module - Various retry delay strategies:
- Fixed interval
- Exponential backoff
- Immediate (no delay)
- Random range (requires the
random
feature)
Add this to your Cargo.toml
:
[dependencies]
tryumph = "0.1.0"
To enable both async and random delay:
[dependencies]
tryumph = { version = "0.1.0", features = ["random"] }
use tryumph::sync::retry;
use tryumph::strategy::Exponential;
use std::time::Duration;
fn main() {
// Try to get data from a potentially failing API with exponential backoff
let result = retry(Exponential::from_millis(100).take(3), || {
// Your operation that may fail
let response = make_api_request();
if response.is_success() {
Ok(response.data)
} else {
Err(response.error)
}
});
match result {
Ok(data) => println!("Successfully retrieved data: {:?}", data),
Err(e) => println!("Failed after multiple retries: {:?}", e),
}
}
// Mock functions for example
fn make_api_request() -> Response {
Response { is_success: true, data: "data", error: "error" }
}
struct Response { is_success: bool, data: &'static str, error: &'static str }
impl Response { fn is_success(&self) -> bool { self.is_success } }
use tryumph::unsync::retry;
use tryumph::strategy::Exponential;
async fn fetch_data() -> Result<String, String> {
// Try to get data from a potentially failing API
let result = retry(Exponential::from_millis(100).take(3), || async {
// Your async operation that may fail
let response = make_api_request().await;
if response.is_success() {
Ok(response.data.to_string())
} else {
Err(response.error.to_string())
}
}).await;
match result {
Ok(data) => {
println!("Successfully retrieved data: {}", data);
Ok(data)
}
Err(e) => {
println!("Failed after multiple retries: {}", e);
Err(e)
}
}
}
use tryumph::sync::retry;
use tryumph::strategy::{Fixed, NoDelay};
// Retry with fixed intervals
let result_fixed = retry(Fixed::from_millis(100).take(5), || {
// Your operation
Ok::<_, &str>("success")
});
// Retry immediately without delays
let result_nodelay = retry(NoDelay.take(3), || {
// Your operation
Ok::<_, &str>("success")
});
random
: Enables randomized delay functionality (depends on rand)
Licensed under either of:
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the MIT license, shall be dual licensed as above, without any additional terms or conditions.