| Crates.io | backoff |
| lib.rs | backoff |
| version | 0.4.0 |
| created_at | 2017-05-28 08:49:16.062452+00 |
| updated_at | 2021-12-14 21:16:45.606935+00 |
| description | Retry operations with exponential backoff policy. |
| homepage | https://github.com/ihrwein/backoff |
| repository | https://github.com/ihrwein/backoff |
| max_upload_size | |
| id | 16581 |
| size | 96,125 |
Exponential backoff and retry.
Inspired by the retry mechanism in Google's google-http-java-client library and its Golang port.
Compile with feature wasm-bindgen or stdweb for use in WASM environments. retry_notify is not yet supported, as it uses std::thread::sleep.
:warning: BREAKING CHANGES: migration instructions under Breaking changes.
backoff is small crate which allows you to retry operations according to backoff policies. It provides:
Just wrap your fallible operation into a closure, and pass it into retry:
use backoff::{retry, ExponentialBackoff, Error};
let op = || {
reqwest::blocking::get("http://example.com").map_err(Error::transient)
};
let _ = retry(&mut ExponentialBackoff::default(), op);
The retry policy will use jitters according to the randomization_factor field of ExponentialBackoff. Check the documentation for more parameters.
Futures are supported by the futures module:
use backoff::ExponentialBackoff;
use backoff::future::retry;
async fn fetch_url(url: &str) -> Result<String, reqwest::Error> {
retry(ExponentialBackoff::default(), || async {
println!("Fetching {}", url);
Ok(reqwest::get(url).await?.text().await?)
})
.await
}
Transient errors got a second field. Useful for handling ratelimits like a HTTP 429 response.
To fix broken code, just replace calls of Error::Transient() with Error::transient().
https://github.com/ihrwein/backoff/pull/28
The Operation trait has been removed, please use normal closures implementing FnMut instead. The retry and retry_notify methods were converted to free functions, available in the crate's root.
https://github.com/ihrwein/backoff/pull/28
The FutureOperation trait has been removed. The retry and retry_notify methods were converted to free functions, available in the crate's root.
stdweb flag was removed, as the project is abandoned.retry, retry_notify taking ownership of Backoff instances (previously &mut)Licensed under either of
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.