mirror of
https://github.com/actix/actix-web.git
synced 2024-11-25 19:11:10 +00:00
optimize Resource and Scope service call (#1867)
This commit is contained in:
parent
a1b00b2cd0
commit
ad608aa64e
2 changed files with 17 additions and 17 deletions
|
@ -11,7 +11,7 @@ use actix_service::boxed::{self, BoxService, BoxServiceFactory};
|
|||
use actix_service::{
|
||||
apply, apply_fn_factory, IntoServiceFactory, Service, ServiceFactory, Transform,
|
||||
};
|
||||
use futures_util::future::{ok, Either, LocalBoxFuture, Ready};
|
||||
use futures_core::future::LocalBoxFuture;
|
||||
|
||||
use crate::data::Data;
|
||||
use crate::dev::{insert_slash, AppService, HttpServiceFactory, ResourceDef};
|
||||
|
@ -524,10 +524,7 @@ impl Service for ResourceService {
|
|||
type Request = ServiceRequest;
|
||||
type Response = ServiceResponse;
|
||||
type Error = Error;
|
||||
type Future = Either<
|
||||
Ready<Result<ServiceResponse, Error>>,
|
||||
LocalBoxFuture<'static, Result<ServiceResponse, Error>>,
|
||||
>;
|
||||
type Future = LocalBoxFuture<'static, Result<ServiceResponse, Error>>;
|
||||
|
||||
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
Poll::Ready(Ok(()))
|
||||
|
@ -539,20 +536,22 @@ impl Service for ResourceService {
|
|||
if let Some(ref data) = self.data {
|
||||
req.add_data_container(data.clone());
|
||||
}
|
||||
return Either::Right(route.call(req));
|
||||
return route.call(req);
|
||||
}
|
||||
}
|
||||
if let Some(ref mut default) = self.default {
|
||||
if let Some(ref data) = self.data {
|
||||
req.add_data_container(data.clone());
|
||||
}
|
||||
Either::Right(default.call(req))
|
||||
default.call(req)
|
||||
} else {
|
||||
let req = req.into_parts().0;
|
||||
Either::Left(ok(ServiceResponse::new(
|
||||
req,
|
||||
Response::MethodNotAllowed().finish(),
|
||||
)))
|
||||
Box::pin(async {
|
||||
Ok(ServiceResponse::new(
|
||||
req,
|
||||
Response::MethodNotAllowed().finish(),
|
||||
))
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
13
src/scope.rs
13
src/scope.rs
|
@ -11,7 +11,7 @@ use actix_service::boxed::{self, BoxService, BoxServiceFactory};
|
|||
use actix_service::{
|
||||
apply, apply_fn_factory, IntoServiceFactory, Service, ServiceFactory, Transform,
|
||||
};
|
||||
use futures_util::future::{ok, Either, LocalBoxFuture, Ready};
|
||||
use futures_core::future::LocalBoxFuture;
|
||||
|
||||
use crate::config::ServiceConfig;
|
||||
use crate::data::Data;
|
||||
|
@ -28,7 +28,6 @@ use crate::service::{
|
|||
type Guards = Vec<Box<dyn Guard>>;
|
||||
type HttpService = BoxService<ServiceRequest, ServiceResponse, Error>;
|
||||
type HttpNewService = BoxServiceFactory<(), ServiceRequest, ServiceResponse, Error, ()>;
|
||||
type BoxedResponse = LocalBoxFuture<'static, Result<ServiceResponse, Error>>;
|
||||
|
||||
/// Resources scope.
|
||||
///
|
||||
|
@ -606,7 +605,7 @@ impl Service for ScopeService {
|
|||
type Request = ServiceRequest;
|
||||
type Response = ServiceResponse;
|
||||
type Error = Error;
|
||||
type Future = Either<BoxedResponse, Ready<Result<Self::Response, Self::Error>>>;
|
||||
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;
|
||||
|
||||
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
Poll::Ready(Ok(()))
|
||||
|
@ -628,15 +627,17 @@ impl Service for ScopeService {
|
|||
if let Some(ref data) = self.data {
|
||||
req.add_data_container(data.clone());
|
||||
}
|
||||
Either::Left(srv.call(req))
|
||||
srv.call(req)
|
||||
} else if let Some(ref mut default) = self.default {
|
||||
if let Some(ref data) = self.data {
|
||||
req.add_data_container(data.clone());
|
||||
}
|
||||
Either::Left(default.call(req))
|
||||
default.call(req)
|
||||
} else {
|
||||
let req = req.into_parts().0;
|
||||
Either::Right(ok(ServiceResponse::new(req, Response::NotFound().finish())))
|
||||
Box::pin(async {
|
||||
Ok(ServiceResponse::new(req, Response::NotFound().finish()))
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue