futures_backoff/
lib.rs

1#![deny(missing_docs)]
2#![deny(warnings)]
3#![deny(missing_debug_implementations)]
4
5//! This library provides asynchronous retry strategies
6//! for use with the popular [`futures`](https://crates.io/crates/futures) crate.
7//!
8//! # Installation
9//!
10//! Add this to your `Cargo.toml`:
11//!
12//! ```toml
13//! [dependencies]
14//! futures-backoff = "0.1"
15//! ```
16//!
17//! # Examples
18//!
19//! ```rust
20//! extern crate futures;
21//! extern crate futures_backoff;
22//!
23//! use futures::{Future, future};
24//! use futures_backoff::retry;
25//!
26//! fn main() {
27//!     let future = retry(|| {
28//!         // do some real-world stuff here...
29//!         future::ok::<u32, ::std::io::Error>(42)
30//!     });
31//!
32//!     let result = future.wait();
33//!
34//!     assert_eq!(result.unwrap(), 42);
35//! }
36//! ```
37
38extern crate futures;
39extern crate futures_timer;
40extern crate rand;
41
42mod action;
43mod condition;
44mod strategy;
45mod future;
46
47pub use action::Action;
48pub use condition::Condition;
49pub use strategy::Strategy;
50pub use future::{Retry, RetryIf};
51
52/// Run the given action, and retry on failure.
53///
54/// Uses the default retry strategy with exponential backoff and a maximum of 5 retry attempts.
55///
56/// To customize the retry strategy, take a look at [`Strategy`](./struct.Strategy.html).
57///
58/// # Example
59///
60/// ```rust
61/// # extern crate futures;
62/// # extern crate futures_backoff;
63/// # use futures::{Future, future};
64/// # use futures_backoff::retry;
65/// #
66/// # fn main() {
67/// let future = retry(|| {
68///     // do some real-world stuff here...
69///     future::ok::<u32, ::std::io::Error>(42)
70/// });
71/// #
72/// # assert_eq!(future.wait().unwrap(), 42);
73/// # }
74/// ```
75pub fn retry<A: Action>(action: A) -> Retry<A> {
76    Strategy::default().retry(action)
77}
78
79/// Run the given action, and retry on failure if the error satisfies a given condition.
80///
81/// Uses the default retry strategy with exponential backoff and a maximum of 5 retry attempts.
82///
83/// To customize the retry strategy, take a look at [`Strategy`](./struct.Strategy.html).
84///
85/// # Example
86///
87/// ```rust
88/// # extern crate futures;
89/// # extern crate futures_backoff;
90/// # use std::io::{Error, ErrorKind};
91/// # use futures::{Future, future};
92/// # use futures_backoff::retry_if;
93/// #
94/// # fn main() {
95/// let future = retry_if(|| {
96///     // do some real-world stuff here...
97///     future::ok(42)
98/// }, |err: &Error| err.kind() == ErrorKind::TimedOut);
99/// #
100/// # assert_eq!(future.wait().unwrap(), 42);
101/// # }
102/// ```
103pub fn retry_if<A: Action, C>(action: A, condition: C) -> RetryIf<A, C>
104    where C: Condition<A::Error>
105{
106    Strategy::default().retry_if(action, condition)
107}