Improve performance of BoxRoute (#339)

* Improve performance of `BoxRoute`

This is based on #315 but slightly shorter.

It removes the need for a `tower::buffer::Buffer` in `BoxRoute` which
improves performance.

* changelog

Co-authored-by: Programatik <[email protected]>
This commit is contained in:
David Pedersen
2021-10-02 13:43:59 +00:00
committed by GitHub
co-authored by Programatik
parent 7fae35020a
commit cc4ae6b297
6 changed files with 85 additions and 214 deletions
+5 -9
View File
@@ -3,7 +3,7 @@
use self::future::{BoxRouteFuture, EmptyRouterFuture, NestedFuture, RouteFuture};
use crate::{
body::{box_body, BoxBody},
buffer::MpscBuffer,
clone_box_service::CloneBoxService,
extract::{
connect_info::{Connected, IntoMakeServiceWithConnectInfo},
OriginalUri,
@@ -24,10 +24,7 @@ use std::{
sync::Arc,
task::{Context, Poll},
};
use tower::{
util::{BoxService, ServiceExt},
ServiceBuilder,
};
use tower::{util::ServiceExt, ServiceBuilder};
use tower_http::map_response_body::MapResponseBodyLayer;
use tower_layer::Layer;
use tower_service::Service;
@@ -256,7 +253,7 @@ impl<S> Router<S> {
/// routes.
pub fn boxed<ReqBody, ResBody>(self) -> Router<BoxRoute<ReqBody, S::Error>>
where
S: Service<Request<ReqBody>, Response = Response<ResBody>> + Send + 'static,
S: Service<Request<ReqBody>, Response = Response<ResBody>> + Clone + Send + Sync + 'static,
S::Error: Into<BoxError> + Send,
S::Future: Send,
ReqBody: Send + 'static,
@@ -266,8 +263,7 @@ impl<S> Router<S> {
self.map(|svc| {
ServiceBuilder::new()
.layer_fn(BoxRoute)
.layer_fn(MpscBuffer::new)
.layer(BoxService::layer())
.layer_fn(CloneBoxService::new)
.layer(MapResponseBodyLayer::new(box_body))
.service(svc)
})
@@ -834,7 +830,7 @@ type Captures = Vec<(String, String)>;
///
/// See [`Router::boxed`] for more details.
pub struct BoxRoute<B = crate::body::Body, E = Infallible>(
MpscBuffer<BoxService<Request<B>, Response<BoxBody>, E>, Request<B>>,
CloneBoxService<Request<B>, Response<BoxBody>, E>,
);
impl<B, E> Clone for BoxRoute<B, E> {