1
0
mirror of https://github.com/matrix-org/matrix-authentication-service.git synced 2025-11-24 23:01:05 +03:00

Use new tuple Layer impls instead of ServiceBuilder (#475)

Co-authored-by: Quentin Gliech <quenting@element.io>
This commit is contained in:
Jonas Platte
2022-10-17 16:48:12 +02:00
committed by GitHub
parent 51515358f7
commit cf6d5a076a
7 changed files with 62 additions and 127 deletions

View File

@@ -116,15 +116,21 @@ pub struct CatchHttpCodesLayer<M> {
mapper: M,
}
impl<M> CatchHttpCodesLayer<M> {
impl<M> CatchHttpCodesLayer<M>
where
M: Clone,
{
pub fn new<B>(bounds: B, mapper: M) -> Self
where
B: RangeBounds<StatusCode>,
M: Clone,
{
let bounds = (bounds.start_bound().cloned(), bounds.end_bound().cloned());
Self { bounds, mapper }
}
pub fn exact(status_code: StatusCode, mapper: M) -> Self {
Self::new(status_code..=status_code, mapper)
}
}
impl<S, M> Layer<S> for CatchHttpCodesLayer<M>

View File

@@ -17,7 +17,7 @@ use std::{marker::PhantomData, time::Duration};
use http::{header::USER_AGENT, HeaderValue, Request, Response};
use tower::{
limit::ConcurrencyLimitLayer, timeout::TimeoutLayer, util::BoxCloneService, Layer, Service,
ServiceBuilder, ServiceExt,
ServiceExt,
};
use tower_http::{
decompression::{DecompressionBody, DecompressionLayer},
@@ -65,21 +65,19 @@ where
// - the TimeoutLayer
// - the DecompressionLayer
// Those layers do type erasure of the error.
ServiceBuilder::new()
.layer(DecompressionLayer::new())
.layer(SetRequestHeaderLayer::overriding(
USER_AGENT,
MAS_USER_AGENT.clone(),
))
(
DecompressionLayer::new(),
SetRequestHeaderLayer::overriding(USER_AGENT, MAS_USER_AGENT.clone()),
// A trace that has the whole operation, with all the redirects, timeouts and rate
// limits in it
.layer(TraceLayer::http_client(self.operation))
.layer(ConcurrencyLimitLayer::new(10))
.layer(FollowRedirectLayer::new())
TraceLayer::http_client(self.operation),
ConcurrencyLimitLayer::new(10),
FollowRedirectLayer::new(),
// A trace for each "real" http request
.layer(TraceLayer::inner_http_client())
.layer(TimeoutLayer::new(Duration::from_secs(10)))
.service(inner)
TraceLayer::inner_http_client(),
TimeoutLayer::new(Duration::from_secs(10)),
)
.layer(inner)
.boxed_clone()
}
}

View File

@@ -16,8 +16,8 @@ use std::marker::PhantomData;
use http::{Request, Response};
use opentelemetry::KeyValue;
use tower::{util::BoxCloneService, Layer, Service, ServiceBuilder, ServiceExt};
use tower_http::{compression::CompressionBody, ServiceBuilderExt};
use tower::{util::BoxCloneService, Layer, Service, ServiceExt};
use tower_http::compression::{CompressionBody, CompressionLayer};
use super::otel::TraceLayer;
@@ -49,21 +49,18 @@ where
type Service = BoxCloneService<Request<ReqBody>, Response<CompressionBody<ResBody>>, S::Error>;
fn layer(&self, inner: S) -> Self::Service {
let builder = ServiceBuilder::new().compression();
let compression = CompressionLayer::new();
#[cfg(feature = "axum")]
let mut trace_layer = TraceLayer::axum();
let mut trace = TraceLayer::axum();
#[cfg(not(feature = "axum"))]
let mut trace_layer = TraceLayer::http_server();
let mut trace = TraceLayer::http_server();
if let Some(name) = &self.listener_name {
trace_layer =
trace_layer.with_static_attribute(KeyValue::new("listener", name.clone()));
trace = trace.with_static_attribute(KeyValue::new("listener", name.clone()));
}
let builder = builder.layer(trace_layer);
builder.service(inner).boxed_clone()
(compression, trace).layer(inner).boxed_clone()
}
}