[][src]Crate again

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.

again::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.

again::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.

This example is not tested
use again::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

RetryPolicy

A template for configuring retry behavior

Traits

Condition

A type to determine if a failed Future should be retried

Task

A unit of work to be retried

Functions

retry

Retries a fallible Future with a default RetryPolicy

retry_if

Retries a fallible Future under a certain provided conditions with a default RetryPolicy