Module tokio_retry::middleware [] [src]

[]

Middleware for tokio services that adds automatic retries in case of failure.

Examples

extern crate futures;
extern crate tokio_core;
extern crate tokio_service;
extern crate tokio_retry;

use std::io;

use futures::{BoxFuture, Future, future};
use tokio_core::reactor::Core;
use tokio_service::Service;
use tokio_retry::Middleware;
use tokio_retry::strategy::{ExponentialBackoff, jitter};

struct EchoService;

impl Service for EchoService {
   type Request = String;
   type Response = String;
   type Error = ();
   type Future = BoxFuture<String, ()>;
   fn call(&self, input: String) -> Self::Future {
       future::ok(input).boxed()
   }
}

fn main() {
    let mut core = Core::new().unwrap();

    let retry_strategy = || ExponentialBackoff::from_millis(10)
        .map(jitter)
        .take(3);

    let retry_service = Middleware::new(core.handle(), retry_strategy, EchoService);
    let retry_result = core.run(retry_service.call("hello world!".to_string()));

    assert_eq!(retry_result, Ok("hello world!".to_string()));
}

Structs

Middleware

Middleware that adds retries to a service via a retry strategy.

ServiceRequest

Represents a retryable request to a service.

Traits

StrategyFactory

Trait to produce iterators that will be used as retry strategies.