Git Product home page Git Product logo

futures-retry's Introduction

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 = listener.incoming()
    .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.
  # 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

futures-retry's People

Contributors

mexus avatar evq avatar xoac avatar wg avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.