3 releases

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

#164 in WebAssembly

Download history 24156/week @ 2024-06-14 24894/week @ 2024-06-21 21121/week @ 2024-06-28 16653/week @ 2024-07-05 14352/week @ 2024-07-12 16090/week @ 2024-07-19 16485/week @ 2024-07-26 15455/week @ 2024-08-02 14769/week @ 2024-08-09 16405/week @ 2024-08-16 15921/week @ 2024-08-23 18124/week @ 2024-08-30 18116/week @ 2024-09-06 13749/week @ 2024-09-13 18372/week @ 2024-09-20 15443/week @ 2024-09-27

69,528 downloads per month
Used in 17 crates (7 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.6–5.5MB
~100K SLoC