11 unstable releases

0.6.0 Jan 13, 2021
0.5.0 Feb 25, 2020
0.4.0 Dec 21, 2019
0.3.3 Apr 27, 2019
0.1.2 Mar 21, 2018

#302 in Asynchronous

Download history 29704/week @ 2024-05-17 28197/week @ 2024-05-24 27062/week @ 2024-05-31 29065/week @ 2024-06-07 24544/week @ 2024-06-14 32662/week @ 2024-06-21 27870/week @ 2024-06-28 19893/week @ 2024-07-05 18572/week @ 2024-07-12 20282/week @ 2024-07-19 18473/week @ 2024-07-26 18972/week @ 2024-08-02 22644/week @ 2024-08-09 21078/week @ 2024-08-16 29048/week @ 2024-08-23 22083/week @ 2024-08-30

99,396 downloads per month
Used in 26 crates (12 directly)

MIT/Apache

27KB
318 lines

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

#[tokio::main]
async fn main() {
  let addr = //...
  # "127.0.0.1:12345";
  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)| {
    tokio::spawn(serve_connection(stream));
    ok(())
  })
  .try_for_each(|_| ok(()))
  .map_err(|(e, _attempt)| eprintln!("Caught an error {}", e));
  # // This nasty hack is required to exit immediately when running the doc tests.
  # futures::pin_mut!(server);
  # let server = select(ok::<_, ()>(()), server).map(|_| ());
  server.await
}

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.

License: MIT/Apache-2.0

Dependencies

~2.6–4MB
~61K SLoC