diff options
| author | mo khan <mo@mokhan.ca> | 2025-07-02 18:36:06 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-07-02 18:36:06 -0600 |
| commit | 8cdfa445d6629ffef4cb84967ff7017654045bc2 (patch) | |
| tree | 22f0b0907c024c78d26a731e2e1f5219407d8102 /vendor/tower/src/timeout/mod.rs | |
| parent | 4351c74c7c5f97156bc94d3a8549b9940ac80e3f (diff) | |
chore: add vendor directory
Diffstat (limited to 'vendor/tower/src/timeout/mod.rs')
| -rw-r--r-- | vendor/tower/src/timeout/mod.rs | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/vendor/tower/src/timeout/mod.rs b/vendor/tower/src/timeout/mod.rs new file mode 100644 index 00000000..da3bbf98 --- /dev/null +++ b/vendor/tower/src/timeout/mod.rs @@ -0,0 +1,70 @@ +//! Middleware that applies a timeout to requests. +//! +//! If the response does not complete within the specified timeout, the response +//! will be aborted. + +pub mod error; +pub mod future; +mod layer; + +pub use self::layer::TimeoutLayer; + +use self::future::ResponseFuture; +use std::task::{Context, Poll}; +use std::time::Duration; +use tower_service::Service; + +/// Applies a timeout to requests. +#[derive(Debug, Clone)] +pub struct Timeout<T> { + inner: T, + timeout: Duration, +} + +// ===== impl Timeout ===== + +impl<T> Timeout<T> { + /// Creates a new [`Timeout`] + pub const fn new(inner: T, timeout: Duration) -> Self { + Timeout { inner, timeout } + } + + /// Get a reference to the inner service + pub fn get_ref(&self) -> &T { + &self.inner + } + + /// Get a mutable reference to the inner service + pub fn get_mut(&mut self) -> &mut T { + &mut self.inner + } + + /// Consume `self`, returning the inner service + pub fn into_inner(self) -> T { + self.inner + } +} + +impl<S, Request> Service<Request> for Timeout<S> +where + S: Service<Request>, + S::Error: Into<crate::BoxError>, +{ + type Response = S::Response; + type Error = crate::BoxError; + type Future = ResponseFuture<S::Future>; + + fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { + match self.inner.poll_ready(cx) { + Poll::Pending => Poll::Pending, + Poll::Ready(r) => Poll::Ready(r.map_err(Into::into)), + } + } + + fn call(&mut self, request: Request) -> Self::Future { + let response = self.inner.call(request); + let sleep = tokio::time::sleep(self.timeout); + + ResponseFuture::new(response, sleep) + } +} |
