3 releases

0.1.2 May 31, 2020
0.1.1 May 30, 2020
0.1.0 May 12, 2020

#462 in WebAssembly

Download history 19735/week @ 2024-12-14 4647/week @ 2024-12-21 4874/week @ 2024-12-28 19452/week @ 2025-01-04 23487/week @ 2025-01-11 20812/week @ 2025-01-18 20526/week @ 2025-01-25 23452/week @ 2025-02-01 27791/week @ 2025-02-08 17151/week @ 2025-02-15 17947/week @ 2025-02-22 19913/week @ 2025-03-01 21542/week @ 2025-03-08 20115/week @ 2025-03-15 22406/week @ 2025-03-22 17911/week @ 2025-03-29

85,787 downloads per month
Used in 16 crates (6 directly)

MIT license

19KB
329 lines

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

Dependencies

~2.4–5MB
~85K SLoC