1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-06-11 17:59:35 +00:00

optimize Resource and Scope service call (#1867)

This commit is contained in:
fakeshadow 2021-01-03 03:40:31 +08:00 committed by GitHub
parent a1b00b2cd0
commit ad608aa64e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 17 deletions

View file

@ -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(),
))
})
}
}
}

View file

@ -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()))
})
}
}
}