retrier

Crates.ioretrier
lib.rsretrier
version0.1.3
sourcesrc
created_at2024-07-02 08:33:01.122329
updated_at2024-07-02 08:37:36.922325
descriptionA wasm-compatible retry library for futures
homepagehttps://github.com/ifiokjr/retrier
repositoryhttps://github.com/ifiokjr/retrier
max_upload_size
id1289362
size76,611
Ifiok Jr. (ifiokjr)

documentation

https://docs.rs/retrier

README

retrier ♻️

A wasm-compatible retry library for futures


A goal of any operation should be a successful outcome. This crate gives operations a better chance at achieving that.

📦 install

In your Cargo.toml file, add the following under the [dependencies] heading

retrier = "0.1"

📝note

This is fork of the again library. This fork brings it up to date and adds to automation features which should make maintainance simpler.

🤸usage

For very simple cases you can use the module level retry function to retry a potentially fallible operation.

use std::error::Error;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    pretty_env_logger::init();
    retrier::retry(|| reqwest::get("https://api.you.com")).await?;
    Ok(())
}

You may not want to retry every kind of error. For preciseness you can be more explicit in which kinds of errors should be retried with the module level retry_if function.

use std::error::Error;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    pretty_env_logger::init();
    retrier::retry_if(
      || reqwest::get("https://api.you.com")
      reqwest::Error::is_status
    ).await?;
    Ok(())
}

You can also customize retry behavior to suit your applications needs with a configurable and reusable RetryPolicy.

use std::error::Error;
use std::time::Duration;
use retrier::RetryPolicy;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    pretty_env_logger::init();
    let policy = RetryPolicy::exponential(Duration::from_millis(200))
      .with_max_retries(10)
      .with_jitter(true);
    policy.retry(|| reqwest::get("https://api.you.com")).await?;
    Ok(())
}

See the docs for more examples.

Commit count: 56

cargo fmt