#backoff #retry #exponential #policy

backoff

Retry operations with exponential backoff policy

11 releases

0.4.0 Dec 14, 2021
0.3.0 Feb 2, 2021
0.2.1 Jul 15, 2020
0.1.6 Jan 19, 2020
0.1.2 May 28, 2017

#6 in Network programming

Download history 26958/week @ 2021-09-29 27306/week @ 2021-10-06 29160/week @ 2021-10-13 28937/week @ 2021-10-20 28085/week @ 2021-10-27 28451/week @ 2021-11-03 32055/week @ 2021-11-10 28360/week @ 2021-11-17 27035/week @ 2021-11-24 77031/week @ 2021-12-01 98717/week @ 2021-12-08 86978/week @ 2021-12-15 57473/week @ 2021-12-22 56867/week @ 2021-12-29 87205/week @ 2022-01-05 84098/week @ 2022-01-12

301,659 downloads per month
Used in 110 crates (58 directly)

MIT/Apache

42KB
677 lines

backoff

Exponential backoff and retry.

Inspired by the retry mechanism in Google's google-http-java-client library and its Golang port.

Build Status crates.io Documentation

Compile with feature wasm-bindgen or stdweb for use in WASM environments. retry_notify is not yet supported, as it uses std::thread::sleep.

⚠️ BREAKING CHANGES: migration instructions under Breaking changes.

Overview

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.

Sync example

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.

Async example

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
}

Breaking changes

0.3.x -> 0.4.x

Adding new field to Error::Transient

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().

0.2.x -> 0.3.x

Removal of Operation trait

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.

Example.

Removal of FutureOperation trait

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.

Example.

Changes in feature flags

  • stdweb flag was removed, as the project is abandoned.

retry, retry_notify taking ownership of Backoff instances (previously &mut)

Example.

License

Licensed under either of

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

~0.4–1.6MB
~30K SLoC