From b4639d593b38a49db77ca6c187dbc8a1efe4f381 Mon Sep 17 00:00:00 2001 From: ihciah Date: Wed, 27 Nov 2024 08:34:19 +0000 Subject: [PATCH] rename --- monolake-core/src/http/mod.rs | 4 +- monolake-services/src/common/mod.rs | 2 +- .../src/common/{route.rs => selector.rs} | 24 ++++----- monolake-services/src/http/handlers/route.rs | 54 +++++-------------- monolake-services/src/http/mod.rs | 2 +- monolake-services/src/http/util.rs | 27 +++++++++- .../src/thrift/handlers/proxy.rs | 4 +- 7 files changed, 58 insertions(+), 59 deletions(-) rename monolake-services/src/common/{route.rs => selector.rs} (94%) diff --git a/monolake-core/src/http/mod.rs b/monolake-core/src/http/mod.rs index 68f304e..ea1c9a7 100644 --- a/monolake-core/src/http/mod.rs +++ b/monolake-core/src/http/mod.rs @@ -50,8 +50,8 @@ pub trait HttpError { } #[derive(Debug, Clone, Default, Copy, PartialEq, Eq)] -pub struct UnrecoverableError(pub E); -impl HttpError for UnrecoverableError { +pub struct HttpFatalError(pub E); +impl HttpError for HttpFatalError { #[inline] fn to_response(&self) -> Option> { None diff --git a/monolake-services/src/common/mod.rs b/monolake-services/src/common/mod.rs index b9ca61f..94138eb 100644 --- a/monolake-services/src/common/mod.rs +++ b/monolake-services/src/common/mod.rs @@ -6,7 +6,7 @@ pub mod detect; pub mod erase; pub mod map; pub mod panic; -pub mod route; +pub mod selector; pub mod timeout; // TODO: remove following re-exports diff --git a/monolake-services/src/common/route.rs b/monolake-services/src/common/selector.rs similarity index 94% rename from monolake-services/src/common/route.rs rename to monolake-services/src/common/selector.rs index b4f0ebb..8b6f3e8 100644 --- a/monolake-services/src/common/route.rs +++ b/monolake-services/src/common/selector.rs @@ -12,7 +12,7 @@ use service_async::Service; /// Generic synchronous selector. /// /// It abstracts the way to select a service or endpoint, including routing and load balancing. -pub trait Selector { +pub trait Select { /// Select output which can be a reference or a owned type. /// /// When the usage style is like select a Service and call it, the output can be a reference. @@ -50,7 +50,7 @@ impl RandomSelector { } } -impl Selector for RandomSelector { +impl Select for RandomSelector { type Output<'a> = &'a T where @@ -150,7 +150,7 @@ impl WeightedRandomSelector { } } -impl Selector for WeightedRandomSelector { +impl Select for WeightedRandomSelector { type Output<'a> = &'a T where @@ -183,7 +183,7 @@ impl RoundRobinSelector { } } -impl Selector for RoundRobinSelector { +impl Select for RoundRobinSelector { type Output<'a> = &'a T where @@ -201,7 +201,7 @@ impl Selector for RoundRobinSelector { #[derive(Debug, Clone)] pub struct IdentitySelector(pub T); -impl Selector for IdentitySelector { +impl Select for IdentitySelector { type Output<'a> = &'a T where @@ -293,7 +293,7 @@ impl LoadBalancer { } } -impl Selector for LoadBalancer { +impl Select for LoadBalancer { type Output<'a> = &'a T where @@ -334,11 +334,11 @@ impl, ESVC: HttpError> HttpError for SelectError(pub S); +pub struct ServiceSelector(pub S); -impl Service for SvcDispatch +impl Service for ServiceSelector where - SEL: Selector, + SEL: Select, for<'a> SEL::Output<'a>: Service, { type Response = SR; @@ -353,7 +353,7 @@ where /// Route service based on the selector. /// /// Get the selector output and call the service with (Req, Out). -pub struct SvcRoute { +pub struct ServiceRouter { pub selector: SEL, pub selector_mapper: F, pub svc: SVC, @@ -364,10 +364,10 @@ pub trait Mapping { fn map<'a>(&self, input: &'a In) -> &'a Self::Out; } -impl Service<(R, CX)> for SvcRoute +impl Service<(R, CX)> for ServiceRouter where F: Mapping, - SEL: Selector, + SEL: Select, for<'a> SVC: Service<(R, SEL::Output<'a>, CX), Response = SVCR, Error = SVCE>, { type Response = SVCR; diff --git a/monolake-services/src/http/handlers/route.rs b/monolake-services/src/http/handlers/route.rs index 6166f11..c980c7b 100644 --- a/monolake-services/src/http/handlers/route.rs +++ b/monolake-services/src/http/handlers/route.rs @@ -100,7 +100,7 @@ use http::{uri::Scheme, HeaderValue, Request, Response, StatusCode}; use monoio_http::common::body::FixedBody; use monolake_core::{ - http::{HttpError, HttpHandler, ResponseWithContinue, UnrecoverableError}, + http::{HttpError, HttpFatalError, HttpHandler, ResponseWithContinue}, util::uri_serde, AnyError, }; @@ -111,11 +111,11 @@ use service_async::{ }; use crate::{ - common::route::{ - IntoWeightedEndpoint, LoadBalanceError, LoadBalanceStrategy, LoadBalancer, Mapping, - Selector, SvcRoute, + common::selector::{ + IntoWeightedEndpoint, LoadBalanceError, LoadBalanceStrategy, LoadBalancer, Mapping, Select, + ServiceRouter, }, - http::generate_response, + http::{generate_response, util::HttpErrorResponder}, }; #[derive(Debug)] @@ -151,9 +151,9 @@ impl HttpError for RouterError { } } -impl Selector for Router +impl Select for Router where - T: Selector, + T: Select, { type Output<'a> = T::Output<'a> @@ -183,7 +183,7 @@ where H: HttpHandler, { type Response = ResponseWithContinue; - type Error = UnrecoverableError; + type Error = HttpFatalError; #[inline] async fn call( @@ -191,11 +191,7 @@ where (mut request, ep, cx): (Request, &'a Endpoint, CX), ) -> Result { rewrite_request(&mut request, ep); - return self - .inner - .handle(request, cx) - .await - .map_err(UnrecoverableError); + return self.inner.handle(request, cx).await.map_err(HttpFatalError); } } @@ -213,8 +209,9 @@ pub struct RewriteAndRouteHandlerFactory { routes: Vec, } -pub type RewriteAndRouteHandler = - HttpErrorResponder>, RewriteHandler, PathExtractor>>; +pub type RewriteAndRouteHandler = HttpErrorResponder< + ServiceRouter>, RewriteHandler, PathExtractor>, +>; #[derive(thiserror::Error, Debug)] pub enum RoutingFactoryError { @@ -232,7 +229,7 @@ impl MakeService for RewriteAndRouteHandlerFactory { fn make_via_ref(&self, old: Option<&Self::Service>) -> Result { let router = Router::new_from_iter(self.routes.clone())?; - Ok(HttpErrorResponder(SvcRoute { + Ok(HttpErrorResponder(ServiceRouter { svc: RewriteHandler { inner: self .inner @@ -257,7 +254,7 @@ where old: Option<&Self::Service>, ) -> Result { let router = Router::new_from_iter(self.routes.clone())?; - Ok(HttpErrorResponder(SvcRoute { + Ok(HttpErrorResponder(ServiceRouter { svc: RewriteHandler { inner: self .inner @@ -271,29 +268,6 @@ where } } -pub struct HttpErrorResponder(pub T); -impl Service<(Request, CX)> for HttpErrorResponder -where - T: HttpHandler, - T::Error: HttpError, -{ - type Response = ResponseWithContinue; - type Error = T::Error; - - async fn call(&self, (req, cx): (Request, CX)) -> Result { - match self.0.handle(req, cx).await { - Ok(resp) => Ok(resp), - Err(e) => { - if let Some(r) = e.to_response() { - Ok((r, true)) - } else { - Err(e) - } - } - } - } -} - #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(rename_all = "UPPERCASE")] #[derive(Default)] diff --git a/monolake-services/src/http/mod.rs b/monolake-services/src/http/mod.rs index 8e7466c..f21f71f 100644 --- a/monolake-services/src/http/mod.rs +++ b/monolake-services/src/http/mod.rs @@ -49,7 +49,7 @@ pub mod handlers; pub mod core; pub mod detect; -mod util; +pub mod util; pub(crate) const CLOSE: &str = "close"; pub(crate) const KEEPALIVE: &str = "Keep-Alive"; diff --git a/monolake-services/src/http/util.rs b/monolake-services/src/http/util.rs index 12fb2b5..b34fae8 100644 --- a/monolake-services/src/http/util.rs +++ b/monolake-services/src/http/util.rs @@ -1,7 +1,9 @@ use std::{future::Future, task::Poll}; -use http::{HeaderValue, Response, StatusCode}; +use http::{HeaderValue, Request, Response, StatusCode}; use monoio_http::common::body::FixedBody; +use monolake_core::http::{HttpError, HttpHandler, ResponseWithContinue}; +use service_async::Service; pin_project_lite::pin_project! { /// AccompanyPair for http decoder and processor. @@ -96,3 +98,26 @@ pub(crate) fn generate_response(status_code: StatusCode, close: bo headers.insert(http::header::CONTENT_LENGTH, HeaderValue::from_static("0")); resp.body(B::fixed_body(None)).unwrap() } + +pub struct HttpErrorResponder(pub T); +impl Service<(Request, CX)> for HttpErrorResponder +where + T: HttpHandler, + T::Error: HttpError, +{ + type Response = ResponseWithContinue; + type Error = T::Error; + + async fn call(&self, (req, cx): (Request, CX)) -> Result { + match self.0.handle(req, cx).await { + Ok(resp) => Ok(resp), + Err(e) => { + if let Some(r) = e.to_response() { + Ok((r, true)) + } else { + Err(e) + } + } + } + } +} diff --git a/monolake-services/src/thrift/handlers/proxy.rs b/monolake-services/src/thrift/handlers/proxy.rs index 515f7c9..962d60f 100644 --- a/monolake-services/src/thrift/handlers/proxy.rs +++ b/monolake-services/src/thrift/handlers/proxy.rs @@ -62,8 +62,8 @@ use monolake_core::{ use serde::{Deserialize, Serialize}; use service_async::{AsyncMakeService, MakeService, ParamMaybeRef, ParamRef, Service}; -use crate::common::route::{ - IntoWeightedEndpoint, LoadBalanceError, LoadBalanceStrategy, LoadBalancer, Selector, +use crate::common::selector::{ + IntoWeightedEndpoint, LoadBalanceError, LoadBalanceStrategy, LoadBalancer, Select, }; pub type PoolThriftConnector = PooledConnector<