https://github.com/EmbarkStudios/tryhard
💫 Easily retry futures 🦀
https://github.com/EmbarkStudios/tryhard
Last synced: over 1 year ago
JSON representation
💫 Easily retry futures 🦀
- Host: GitHub
- URL: https://github.com/EmbarkStudios/tryhard
- Owner: EmbarkStudios
- License: apache-2.0
- Created: 2020-11-14T11:07:28.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2023-08-23T20:07:00.000Z (almost 3 years ago)
- Last Synced: 2025-03-05T00:52:40.795Z (over 1 year ago)
- Language: Rust
- Size: 57.6 KB
- Stars: 205
- Watchers: 5
- Forks: 11
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE-APACHE
- Code of conduct: CODE_OF_CONDUCT.md
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# 💫 tryhard
[](https://embark.dev)
[](https://discord.gg/dAuKfZS)
[](https://crates.io/crates/tryhard)
[](https://docs.rs/tryhard)
[](https://deps.rs/repo/github/EmbarkStudios/tryhard)
[](https://github.com/EmbarkStudios/tryhard/actions)
`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:
```rust
async fn read_file(path: &str) -> Result {
// ...
}
```
Calling that function and retrying at most 10 times with no delay between attempts can be done like so:
```rust
tryhard::retry_fn(|| read_file("Cargo.toml"))
.retries(10)
.await?;
```
You can also retry with a fixed delay between attempts:
```rust
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:
```rust
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](https://docs.rs/tryhard) for more details.
## Contributing
[](../main/CODE_OF_CONDUCT.md)
We welcome community contributions to this project.
Please read our [Contributor Guide](CONTRIBUTING.md) for more information on how to get started.
## License
Licensed under either of
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
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.