tower/timeout/
mod.rs
1pub mod error;
7pub mod future;
8mod layer;
9
10pub use self::layer::TimeoutLayer;
11
12use self::future::ResponseFuture;
13use std::task::{Context, Poll};
14use std::time::Duration;
15use tower_service::Service;
16
17#[derive(Debug, Clone)]
19pub struct Timeout<T> {
20 inner: T,
21 timeout: Duration,
22}
23
24impl<T> Timeout<T> {
27 pub const fn new(inner: T, timeout: Duration) -> Self {
29 Timeout { inner, timeout }
30 }
31
32 pub fn get_ref(&self) -> &T {
34 &self.inner
35 }
36
37 pub fn get_mut(&mut self) -> &mut T {
39 &mut self.inner
40 }
41
42 pub fn into_inner(self) -> T {
44 self.inner
45 }
46}
47
48impl<S, Request> Service<Request> for Timeout<S>
49where
50 S: Service<Request>,
51 S::Error: Into<crate::BoxError>,
52{
53 type Response = S::Response;
54 type Error = crate::BoxError;
55 type Future = ResponseFuture<S::Future>;
56
57 fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
58 match self.inner.poll_ready(cx) {
59 Poll::Pending => Poll::Pending,
60 Poll::Ready(r) => Poll::Ready(r.map_err(Into::into)),
61 }
62 }
63
64 fn call(&mut self, request: Request) -> Self::Future {
65 let response = self.inner.call(request);
66 let sleep = tokio::time::sleep(self.timeout);
67
68 ResponseFuture::new(response, sleep)
69 }
70}