6 releases (breaking)

0.5.1 Aug 23, 2023
0.5.0 Sep 1, 2022
0.4.0 Jun 22, 2021
0.3.0 Jan 7, 2021
0.1.0 Nov 23, 2020

#215 in Rust patterns

Download history 3288/week @ 2023-12-15 1044/week @ 2023-12-22 2421/week @ 2023-12-29 5286/week @ 2024-01-05 6971/week @ 2024-01-12 6555/week @ 2024-01-19 6347/week @ 2024-01-26 8229/week @ 2024-02-02 7297/week @ 2024-02-09 5873/week @ 2024-02-16 7204/week @ 2024-02-23 10025/week @ 2024-03-01 10236/week @ 2024-03-08 10728/week @ 2024-03-15 10298/week @ 2024-03-22 9443/week @ 2024-03-29

42,990 downloads per month
Used in 8 crates (6 directly)

MIT/Apache

38KB
666 lines

💫 tryhard

Embark Embark Crates.io Docs dependency status Build status

tryhard makes it easy to retry futures that might fail. You can control the number of retries, the backoff strategy, and the max duration.

Examples

First imagine you have some async function that can fail:

async fn read_file(path: &str) -> Result<String, std::io::Error> {
    // ...
}

Calling that function and retrying at most 10 times with no delay between attempts can be done like so:

tryhard::retry_fn(|| read_file("Cargo.toml"))
    .retries(10)
    .await?;

You can also retry with a fixed delay between attempts:

tryhard::retry_fn(|| read_file("Cargo.toml"))
    .retries(10)
    .fixed_backoff(Duration::from_millis(100))
    .await?;

Or exponential backoff, where the delay doubles each time, with a max delay of 1 second:

tryhard::retry_fn(|| read_file("Cargo.toml"))
    .retries(10)
    .exponential_backoff(Duration::from_millis(10))
    .max_delay(Duration::from_secs(1))
    .await?;

See the docs for more details.

Contributing

Contributor Covenant

We welcome community contributions to this project.

Please read our Contributor Guide for more information on how to get started.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~3–4.5MB
~80K SLoC