1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-27 17:52:56 +01:00

optimize ErrorHandler middleware (#1902)

This commit is contained in:
fakeshadow 2021-01-14 09:43:44 +08:00 committed by GitHub
parent d34a8689e5
commit 4edeb5ce47
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,10 +1,15 @@
//! For middleware documentation, see [`ErrorHandlers`]. //! For middleware documentation, see [`ErrorHandlers`].
use std::rc::Rc; use std::{
future::Future,
pin::Pin,
rc::Rc,
task::{Context, Poll},
};
use actix_service::{Service, Transform}; use actix_service::{Service, Transform};
use ahash::AHashMap; use ahash::AHashMap;
use futures_util::future::{ok, FutureExt, LocalBoxFuture, Ready}; use futures_core::{future::LocalBoxFuture, ready};
use crate::{ use crate::{
dev::{ServiceRequest, ServiceResponse}, dev::{ServiceRequest, ServiceResponse},
@ -51,9 +56,11 @@ type ErrorHandler<B> = dyn Fn(ServiceResponse<B>) -> Result<ErrorHandlerResponse
/// )); /// ));
/// ``` /// ```
pub struct ErrorHandlers<B> { pub struct ErrorHandlers<B> {
handlers: Rc<AHashMap<StatusCode, Box<ErrorHandler<B>>>>, handlers: Handlers<B>,
} }
type Handlers<B> = Rc<AHashMap<StatusCode, Box<ErrorHandler<B>>>>;
impl<B> Default for ErrorHandlers<B> { impl<B> Default for ErrorHandlers<B> {
fn default() -> Self { fn default() -> Self {
ErrorHandlers { ErrorHandlers {
@ -82,7 +89,7 @@ impl<B> ErrorHandlers<B> {
impl<S, B> Transform<S, ServiceRequest> for ErrorHandlers<B> impl<S, B> Transform<S, ServiceRequest> for ErrorHandlers<B>
where where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>, S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
S::Future: 'static, S::Future: 'static,
B: 'static, B: 'static,
{ {
@ -90,20 +97,18 @@ where
type Error = Error; type Error = Error;
type Transform = ErrorHandlersMiddleware<S, B>; type Transform = ErrorHandlersMiddleware<S, B>;
type InitError = (); type InitError = ();
type Future = Ready<Result<Self::Transform, Self::InitError>>; type Future = LocalBoxFuture<'static, Result<Self::Transform, Self::InitError>>;
fn new_transform(&self, service: S) -> Self::Future { fn new_transform(&self, service: S) -> Self::Future {
ok(ErrorHandlersMiddleware { let handlers = self.handlers.clone();
service, Box::pin(async move { Ok(ErrorHandlersMiddleware { service, handlers }) })
handlers: self.handlers.clone(),
})
} }
} }
#[doc(hidden)] #[doc(hidden)]
pub struct ErrorHandlersMiddleware<S, B> { pub struct ErrorHandlersMiddleware<S, B> {
service: S, service: S,
handlers: Rc<AHashMap<StatusCode, Box<ErrorHandler<B>>>>, handlers: Handlers<B>,
} }
impl<S, B> Service<ServiceRequest> for ErrorHandlersMiddleware<S, B> impl<S, B> Service<ServiceRequest> for ErrorHandlersMiddleware<S, B>
@ -114,35 +119,63 @@ where
{ {
type Response = ServiceResponse<B>; type Response = ServiceResponse<B>;
type Error = Error; type Error = Error;
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>; type Future = ErrorHandlersFuture<S::Future, B>;
actix_service::forward_ready!(service); actix_service::forward_ready!(service);
fn call(&mut self, req: ServiceRequest) -> Self::Future { fn call(&mut self, req: ServiceRequest) -> Self::Future {
let handlers = self.handlers.clone(); let handlers = self.handlers.clone();
let fut = self.service.call(req); let fut = self.service.call(req);
ErrorHandlersFuture::ServiceFuture { fut, handlers }
}
}
async move { #[pin_project::pin_project(project = ErrorHandlersProj)]
let res = fut.await?; pub enum ErrorHandlersFuture<Fut, B>
where
Fut: Future,
{
ServiceFuture {
#[pin]
fut: Fut,
handlers: Handlers<B>,
},
HandlerFuture {
fut: LocalBoxFuture<'static, Fut::Output>,
},
}
if let Some(handler) = handlers.get(&res.status()) { impl<Fut, B> Future for ErrorHandlersFuture<Fut, B>
match handler(res) { where
Ok(ErrorHandlerResponse::Response(res)) => Ok(res), Fut: Future<Output = Result<ServiceResponse<B>, Error>>,
Ok(ErrorHandlerResponse::Future(fut)) => fut.await, {
Err(e) => Err(e), type Output = Fut::Output;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
match self.as_mut().project() {
ErrorHandlersProj::ServiceFuture { fut, handlers } => {
let res = ready!(fut.poll(cx))?;
match handlers.get(&res.status()) {
Some(handler) => match handler(res)? {
ErrorHandlerResponse::Response(res) => Poll::Ready(Ok(res)),
ErrorHandlerResponse::Future(fut) => {
self.as_mut()
.set(ErrorHandlersFuture::HandlerFuture { fut });
self.poll(cx)
} }
} else { },
Ok(res) None => Poll::Ready(Ok(res)),
} }
} }
.boxed_local() ErrorHandlersProj::HandlerFuture { fut } => fut.as_mut().poll(cx),
}
} }
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use actix_service::IntoService; use actix_service::IntoService;
use futures_util::future::ok; use futures_util::future::{ok, FutureExt};
use super::*; use super::*;
use crate::http::{header::CONTENT_TYPE, HeaderValue, StatusCode}; use crate::http::{header::CONTENT_TYPE, HeaderValue, StatusCode};