1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#![doc(html_root_url = "https://docs.rs/tower-timeout/0.1.1")]
#![deny(missing_debug_implementations, missing_docs, rust_2018_idioms)]
#![allow(elided_lifetimes_in_paths)]
#![cfg_attr(test, deny(warnings))]
pub mod error;
pub mod future;
mod layer;
pub use crate::layer::TimeoutLayer;
use crate::{error::Error, future::ResponseFuture};
use futures::Poll;
use tokio_timer::{clock, Delay};
use tower_service::Service;
use std::time::Duration;
#[derive(Debug, Clone)]
pub struct Timeout<T> {
inner: T,
timeout: Duration,
}
impl<T> Timeout<T> {
pub fn new(inner: T, timeout: Duration) -> Self {
Timeout { inner, timeout }
}
}
impl<S, Request> Service<Request> for Timeout<S>
where
S: Service<Request>,
Error: From<S::Error>,
{
type Response = S::Response;
type Error = Error;
type Future = ResponseFuture<S::Future>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
self.inner.poll_ready().map_err(Into::into)
}
fn call(&mut self, request: Request) -> Self::Future {
let response = self.inner.call(request);
let sleep = Delay::new(clock::now() + self.timeout);
ResponseFuture::new(response, sleep)
}
}