Expand description
Retrier 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, retrier 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
retrier 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
retrier 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]
retrier = { version = "x", features = ["js"] }Structs§
- Retry
Policy - A template for configuring retry behavior
Traits§
- Condition
- A type to determine if a failed Future should be retried
- Success
Condition - A type to determine if a successful Future should be retried
- Task
- A unit of work to be retried
- Task
With Parameter - A unit of work to be retried, that accepts a parameter
Functions§
- collect
- Reruns and collects the results of a successful
Futureunder a certain provided condition with a defaultRetryPolicy - collect_
and_ retry - Reruns and collects the results of a
Future, if successful, with a defaultRetryPolicyunder a certain provided success condition. Also retries theFuture, if not successful under the same policy configuration and the provided error condition. - retry
- Retries a fallible
Futurewith a defaultRetryPolicy - retry_
if - Retries a fallible
Futureunder a certain provided condition with a defaultRetryPolicy