Trait futures_retry::StreamRetryExt
[−]
[src]
pub trait StreamRetryExt: Stream { fn into_retry<F, ExtErr>(self, error_action: F) -> StreamRetry<F, Self>
where
F: FnMut(Self::Error) -> RetryPolicy<ExtErr>,
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:
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)), } } fn serve_connection(stream: TcpStream) -> Box<Future<Item = (), Error = ()> + Send> { // ... } fn main() { let listener: TcpListener = // ... let server = listener.incoming() .into_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 into_retry<F, ExtErr>(self, error_action: F) -> StreamRetry<F, Self> where
F: FnMut(Self::Error) -> RetryPolicy<ExtErr>,
Self: Sized,
F: FnMut(Self::Error) -> RetryPolicy<ExtErr>,
Self: Sized,
Converts the stream into a retry stream. See StreamRetry::new
for details.