[][src]Crate futures_retry

pipeline status crates.io docs.rs

[Release docs]

[Master docs]

A tool that helps you retry your future :) Well, Futures and Streams, to be precise.

It's quite a common task when you need to repeat some action if you've got an error, be it a connection timeout or some temporary OS error.

When you do stuff in a synchronous manner it's quite easy to implement the attempts logic, but when it comes to asynchronous programming you suddenly need to write a fool load of a boilerplate code, introduce state machines and everything.

This library aims to make your life easier and let you write more straightword and nice code, concentrating on buisness logic rathen than on handling all the mess.

I was inspired to write this library after coming over a hyper issue, and I came to an understanding that the problem is more common than I used to think.

For examples have a look in the examples/ folder in the git repo.

Suggestions and critiques are welcome!

// ...
use futures_retry::{RetryPolicy, StreamRetryExt};

// In this example we use a free function to handle errors, while in your project you have
// more options: for simple cases a simple closure will do, for complex cases you might go
// as far as implementing an `ErrorHandler` trait for a custom type with some complex logic.
fn handle_error(e: io::Error) -> RetryPolicy<io::Error> {
  match e.kind() {
    io::ErrorKind::Interrupted => RetryPolicy::Repeat,
    io::ErrorKind::PermissionDenied => RetryPolicy::ForwardError(e),
    _ => RetryPolicy::WaitRetry(Duration::from_millis(5)),

async fn serve_connection(stream: TcpStream) {
  // ...

async fn main() {
  let addr = //...
  let mut listener = TcpListener::bind(addr).await.unwrap();
  let server = stream::try_unfold(listener, |listener| async move {
    Ok(Some((listener.accept().await?.0, listener)))
  .retry(handle_error) // Magic happens here
  .and_then(|(stream, _attempt)| {
  .try_for_each(|_| ok(()))
  .map_err(|(e, _attempt)| eprintln!("Caught an error {}", e));


Licensed under either of

at your option.


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.



A future that transparently launches an underlying future (created by a provided factory each time) as many times as needed to get things done.


Provides a way to handle errors during a Stream execution, i.e. it gives you an ability to poll for future stream's items with a delay.



What to do when a future returns an error. Used in FutureRetry::new and StreamRetry::new.



An error handler trait.


A factory trait used to create futures.


An extention trait for Stream which allows to use StreamRetry in a chain-like manner.