Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http: Improve timeout module #2248

Merged
merged 1 commit into from
Feb 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 30 additions & 17 deletions linkerd/proxy/http/src/timeout.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
use linkerd_error::Error;
use linkerd_stack::{layer, MapErr, NewService, Param, Timeout, TimeoutError};
use linkerd_stack::{layer, ExtractParam, MapErr, NewService, Timeout, TimeoutError};
use std::time::Duration;
use thiserror::Error;

#[derive(Clone, Debug)]
pub struct ResponseTimeout(pub Option<Duration>);

#[derive(Clone, Debug, Error)]
#[error("HTTP response timeout after {0:?}")]
pub struct ResponseTimeoutError(Duration);

/// An HTTP-specific optional timeout layer.
///
/// The stack target must implement `HasTimeout`, and if a duration is
Expand All @@ -18,25 +11,45 @@ pub struct ResponseTimeoutError(Duration);
/// Timeout errors are translated into `http::Response`s with appropiate
/// status codes.
#[derive(Clone, Debug)]
pub struct NewTimeout<M> {
inner: M,
pub struct NewTimeout<X, N> {
inner: N,
extract: X,
}

/// Param type configuring a timeout for HTTP responses.
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ResponseTimeout(pub Option<Duration>);

#[derive(Clone, Debug, Error)]
#[error("HTTP response timeout after {0:?}")]
pub struct ResponseTimeoutError(Duration);

// === impl NewTimeout ===

impl<X: Clone, N> NewTimeout<X, N> {
pub fn layer_via(extract: X) -> impl tower::layer::Layer<N, Service = Self> + Clone {
layer::mk(move |inner| Self {
inner,
extract: extract.clone(),
})
}
}

impl<N> NewTimeout<N> {
impl<N> NewTimeout<(), N> {
pub fn layer() -> impl tower::layer::Layer<N, Service = Self> + Clone {
layer::mk(|inner| Self { inner })
Self::layer_via(())
}
}

impl<T, M> NewService<T> for NewTimeout<M>
impl<T, X, N> NewService<T> for NewTimeout<X, N>
where
T: Param<ResponseTimeout>,
M: NewService<T>,
X: ExtractParam<ResponseTimeout, T>,
N: NewService<T>,
{
type Service = MapErr<fn(Error) -> Error, Timeout<M::Service>>;
type Service = MapErr<fn(Error) -> Error, Timeout<N::Service>>;

fn new_service(&self, target: T) -> Self::Service {
let svc = match target.param() {
let svc = match self.extract.extract_param(&target) {
ResponseTimeout(Some(t)) => Timeout::new(self.inner.new_service(target), t),
ResponseTimeout(None) => Timeout::passthru(self.inner.new_service(target)),
};
Expand Down