tower_async/timeout/
mod.rs1pub mod error;
7mod layer;
8
9pub use self::layer::TimeoutLayer;
10
11use error::Elapsed;
12
13use std::time::Duration;
14use tower_async_service::Service;
15
16#[derive(Debug, Clone)]
18pub struct Timeout<T> {
19 inner: T,
20 timeout: Duration,
21}
22
23impl<T> Timeout<T> {
26 pub fn new(inner: T, timeout: Duration) -> Self {
28 Timeout { inner, timeout }
29 }
30
31 pub fn get_ref(&self) -> &T {
33 &self.inner
34 }
35
36 pub fn into_inner(self) -> T {
38 self.inner
39 }
40}
41
42impl<S, Request> Service<Request> for Timeout<S>
43where
44 S: Service<Request>,
45 S::Error: Into<crate::BoxError>,
46{
47 type Response = S::Response;
48 type Error = crate::BoxError;
49
50 async fn call(&self, request: Request) -> Result<Self::Response, Self::Error> {
51 tokio::select! {
52 res = self.inner.call(request) => res.map_err(Into::into),
53 _ = tokio::time::sleep(self.timeout) => Err(Elapsed(()).into()),
54 }
55 }
56}