Giter Club home page Giter Club logo

backoff's Introduction

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

ihrwein#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

ihrwein#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.

backoff's People

Contributors

ihrwein avatar thedodd avatar coolreader18 avatar mitsuhiko avatar zummenix avatar incker avatar andrewbanchich avatar dcormier avatar jplatte avatar stiar avatar nightkr avatar clux avatar tyranron avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.