[][src]Trait futures_retry::StreamRetryExt

pub trait StreamRetryExt: Stream {
    fn retry<F>(self, error_action: F) -> StreamRetry<F, Self>
    where
        Self: Sized
, { ... } }

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

Example

This magic trait allows you to handle errors on streams in a very neat manner:

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

fn serve_connection(stream: TcpStream) -> impl Future<Item = (), Error = ()> + Send {
  // ...
}

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

Provided methods

fn retry<F>(self, error_action: F) -> StreamRetry<F, Self> where
    Self: Sized

Converts the stream into a retry stream. See StreamRetry::new for details.

Loading content...

Implementors

impl<S: ?Sized> StreamRetryExt for S where
    S: Stream
[src]

fn retry<F>(self, error_action: F) -> StreamRetry<F, Self> where
    Self: Sized
[src]

Loading content...