mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-31 00:00:10 +02:00
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:
co-authored by
Programatik
parent
7fae35020a
commit
cc4ae6b297
@@ -1,6 +1,8 @@
|
||||
//! Future types.
|
||||
|
||||
use crate::{body::BoxBody, buffer::MpscBuffer, routing::FromEmptyRouter, BoxError};
|
||||
use crate::{
|
||||
body::BoxBody, clone_box_service::CloneBoxService, routing::FromEmptyRouter, BoxError,
|
||||
};
|
||||
use futures_util::ready;
|
||||
use http::{Request, Response};
|
||||
use pin_project_lite::pin_project;
|
||||
@@ -11,10 +13,7 @@ use std::{
|
||||
pin::Pin,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
use tower::{
|
||||
util::{BoxService, Oneshot},
|
||||
ServiceExt,
|
||||
};
|
||||
use tower::{util::Oneshot, ServiceExt};
|
||||
use tower_service::Service;
|
||||
|
||||
pub use super::or::ResponseFuture as OrResponseFuture;
|
||||
@@ -33,10 +32,7 @@ pin_project! {
|
||||
{
|
||||
#[pin]
|
||||
pub(super) inner: Oneshot<
|
||||
MpscBuffer<
|
||||
BoxService<Request<B>, Response<BoxBody>, E >,
|
||||
Request<B>
|
||||
>,
|
||||
CloneBoxService<Request<B>, Response<BoxBody>, E>,
|
||||
Request<B>,
|
||||
>,
|
||||
}
|
||||
|
||||
+5
-9
@@ -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> {
|
||||
|
||||
Reference in New Issue
Block a user