futures-retry 0.1.4

Retry your Futures and Streams!
Documentation

futures-retry

pipeline status crates.io docs.rs

[Release docs]

[Master docs]

A simple crate that helps you to retry your Futures and Streams in a neat and simple way.

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

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)),
  }
}

// Use `Box<...>` instead of `impl ...` if your rust version doesn't support `impl Trait`.
fn serve_connection(stream: TcpStream) -> impl Future<Item = (), Error = ()> + Send {
  // ...
}

fn main() {
  let listener: TcpListener = // ...
  let server = listener.incoming()
    .retry(handle_error) // Magic happens here
    .and_then(|stream| {
      tokio::spawn(serve_connection(stream));
      Ok(())
    })
    .for_each(|_| Ok(()))
    .map_err(|e| eprintln!("Caught an error {}", e));
  tokio::run(server);
}

License

Licensed under either of

at your option.

Contribution

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.