Crate retrier

source ·
Expand description

Again is a wasm-compatible utility for retrying standard library Futures with a Result output type

A goal of any operation should be a successful outcome. This crate gives operations a better chance at achieving that.

§Examples

§Hello world

For simple cases, you can use the module level retry fn, which will retry a task every second for 5 seconds with an exponential backoff.

retrier::retry(|| reqwest::get("https://api.company.com"));

§Conditional retries

By default, again will retry any failed Future if its Result output type is an Err. You may not want to retry every kind of error. In those cases you may wish to use the retry_if fn, which accepts an additional argument to conditionally determine if the error should be retried.

retrier::retry_if(
    || reqwest::get("https://api.company.com"),
    reqwest::Error::is_status,
);

§Retry policies

Every application has different needs. The default retry behavior in again likely will not suit all of them. You can define your own retry behavior with a RetryPolicy. A RetryPolicy can be configured with a fixed or exponential backoff, jitter, and other common retry options. This objects may be reused across operations. For more information see the RetryPolicy docs.

use retrier::RetryPolicy;
use std::time::Duration;

let policy = RetryPolicy::fixed(Duration::from_millis(100))
    .with_max_retries(10)
    .with_jitter(false);

policy.retry(|| reqwest::get("https://api.company.com"));

§Logging

For visibility on when operations fail and are retried, a log::trace message is emitted, logging the Debug display of the error and the delay before the next attempt.

§wasm features

again supports WebAssembly targets i.e. wasm32-unknown-unknown which should make this crate a good fit for most environments

Two cargo features exist to support various wasm runtimes: wasm-bindgen and stdweb. To enable them add the following to your Cargo.toml file.

[dependencies]
again = { version = "xxx", features = ["wasm-bindgen"] }

Structs§

Traits§

  • A type to determine if a failed Future should be retried
  • A type to determine if a successful Future should be retried
  • A unit of work to be retried
  • A unit of work to be retried, that accepts a parameter

Functions§

  • Reruns and collects the results of a successful Future under a certain provided condition with a default RetryPolicy
  • Reruns and collects the results of a Future, if successful, with a default RetryPolicy under a certain provided success condition. Also retries the Future, if not successful under the same policy configuration and the provided error condition.
  • Retries a fallible Future with a default RetryPolicy
  • Retries a fallible Future under a certain provided condition with a default RetryPolicy