Crate retryx

Crate retryx 

Source
Expand description

Async-first retry & backoff helper for Tokio-based Rust services.

Published v1.0.0

This crate provides a small, opinionated retry() builder that can wrap any async operation returning Result<T, E> and retry it with fixed or exponential backoff.

Minimal example:

use retryx::retry;
use std::time::Duration;

#[derive(Debug, Clone)]
struct MyError;

impl std::fmt::Display for MyError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "my error")
    }
}

impl std::error::Error for MyError {}

impl From<tokio::time::error::Elapsed> for MyError {
    fn from(_: tokio::time::error::Elapsed) -> Self {
        MyError
    }
}

async fn call_api() -> Result<String, MyError> {
    // Your real API call goes here.
    Ok("ok".to_string())
}

#[tokio::main]
async fn main() -> Result<(), MyError> {
    let value = retry::<MyError>()
        .times(3)
        .exponential()
        .timeout(Duration::from_secs(2))
        .run(|| async {
            call_api().await
        })
        .await?;

    println!("Got value: {value}");
    Ok(())
}

Structs§

Retry
Builder for configuring and running async retries.

Functions§

retry
Create a new retry builder with sane defaults.