Coverage Report

Created: 2025-11-16 06:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/tower-0.5.2/src/timeout/mod.rs
Line
Count
Source
1
//! Middleware that applies a timeout to requests.
2
//!
3
//! If the response does not complete within the specified timeout, the response
4
//! will be aborted.
5
6
pub mod error;
7
pub mod future;
8
mod layer;
9
10
pub use self::layer::TimeoutLayer;
11
12
use self::future::ResponseFuture;
13
use std::task::{Context, Poll};
14
use std::time::Duration;
15
use tower_service::Service;
16
17
/// Applies a timeout to requests.
18
#[derive(Debug, Clone)]
19
pub struct Timeout<T> {
20
    inner: T,
21
    timeout: Duration,
22
}
23
24
// ===== impl Timeout =====
25
26
impl<T> Timeout<T> {
27
    /// Creates a new [`Timeout`]
28
0
    pub const fn new(inner: T, timeout: Duration) -> Self {
29
0
        Timeout { inner, timeout }
30
0
    }
31
32
    /// Get a reference to the inner service
33
0
    pub fn get_ref(&self) -> &T {
34
0
        &self.inner
35
0
    }
36
37
    /// Get a mutable reference to the inner service
38
0
    pub fn get_mut(&mut self) -> &mut T {
39
0
        &mut self.inner
40
0
    }
41
42
    /// Consume `self`, returning the inner service
43
0
    pub fn into_inner(self) -> T {
44
0
        self.inner
45
0
    }
46
}
47
48
impl<S, Request> Service<Request> for Timeout<S>
49
where
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
0
    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
58
0
        match self.inner.poll_ready(cx) {
59
0
            Poll::Pending => Poll::Pending,
60
0
            Poll::Ready(r) => Poll::Ready(r.map_err(Into::into)),
61
        }
62
0
    }
63
64
0
    fn call(&mut self, request: Request) -> Self::Future {
65
0
        let response = self.inner.call(request);
66
0
        let sleep = tokio::time::sleep(self.timeout);
67
68
0
        ResponseFuture::new(response, sleep)
69
0
    }
70
}