Exponential backoff and retry. This is a fork of backoff. The parent repo has been unmaintained for a while and this version provides up-to-date dependencies and a MaybeBackoff interface that streamlines conditional backoff usage.
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.
backoff is small crate which allows you to retry operations according to backoff policies. It provides:
- Error type to wrap errors as either transient of permanent,
- different backoff algorithms, including exponential,
- supporting both sync and async code.
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
}Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT) at your option.
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.