Skip to content
Exponential backoff generator with jitter.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github
src
tests
.gitignore
.travis.yml
CERTIFICATE
Cargo.lock
Cargo.toml
LICENSE-APACHE
LICENSE-MIT
README.md
rustfmt.toml

README.md

exponential-backoff

crates.io version build status downloads docs.rs docs

Exponential backoff generator. Serves as a building block to implement custom retry functions.

Why?

When an network requests times out, often the best way to solve it is to try again. But trying again straight away might at best cause some network overhead, and at worst a full fledged DDOS. So we have to be responsible about it.

A good explanation of retry strategies can be found on the Stripe blog.

Usage

Here we try and read a file from disk, and try again if it fails. A more realistic scenario would probably to perform an HTTP request, but the approach should be similar.

extern crate exponential_backoff;

use exponential_backoff::Backoff;
use std::{fs, thread, time::Duration};

let retries = 8;
let backoff = Backoff::new(retries)
  .timeout_range(Duration::from_millis(100), Duration::from_secs(10))
  .jitter(0.3)
  .factor(2);

for duration in &backoff {
  match fs::read_to_string("README.md") {
    Ok(string) => return Ok(string),
    Err(err) => match duration {
      Some(duration) => thread::sleep(duration),
      None => return err,
    }
  }
}

Installation

$ cargo add exponential-backoff

See Also

Further Reading

License

MIT OR Apache-2.0

You can’t perform that action at this time.