again

Crates.ioagain
lib.rsagain
version0.1.2
sourcesrc
created_at2020-05-12 23:01:53.565815
updated_at2020-05-31 00:02:01.197576
descriptionwasm-compatible retry util for std library futures
homepagehttps://github.com/softprops/again
repositoryhttps://github.com/softprops/again
max_upload_size
id240866
size59,088
cargo (github:rustpq:cargo)

documentation

https://docs.rs/again

README

again ♻️

wasm-compatible retry interfaces for fallible Rustlang std library 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

again = "0.1"

🤸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();
    again::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();
    again::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 again::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.

Doug Tangren (softprops) 2020

Commit count: 47

cargo fmt