From 67f627dc930463f50d8927a24b02df63fee69b91 Mon Sep 17 00:00:00 2001 From: taikulawo Date: Wed, 10 Jul 2024 16:45:57 +0800 Subject: [PATCH 1/2] report to upstream --- pingora-core/src/protocols/ssl/mod.rs | 2 +- pingora-proxy/examples/ctx.rs | 2 +- pingora-proxy/examples/gateway.rs | 2 +- pingora-proxy/examples/load_balancer.rs | 2 +- pingora-proxy/examples/modify_response.rs | 2 +- pingora-proxy/examples/multi_lb.rs | 2 +- pingora-proxy/src/lib.rs | 7 +++---- pingora-proxy/src/proxy_trait.rs | 2 +- pingora-proxy/tests/utils/server_utils.rs | 8 +++++--- 9 files changed, 15 insertions(+), 14 deletions(-) diff --git a/pingora-core/src/protocols/ssl/mod.rs b/pingora-core/src/protocols/ssl/mod.rs index f1ce8b95..4f6f928c 100644 --- a/pingora-core/src/protocols/ssl/mod.rs +++ b/pingora-core/src/protocols/ssl/mod.rs @@ -236,7 +236,7 @@ impl ALPN { } } - pub(crate) fn from_wire_selected(raw: &[u8]) -> Option { + pub fn from_wire_selected(raw: &[u8]) -> Option { match raw { b"http/1.1" => Some(Self::H1), b"h2" => Some(Self::H2), diff --git a/pingora-proxy/examples/ctx.rs b/pingora-proxy/examples/ctx.rs index 4838391e..46628b23 100644 --- a/pingora-proxy/examples/ctx.rs +++ b/pingora-proxy/examples/ctx.rs @@ -43,7 +43,7 @@ fn check_beta_user(req: &pingora_http::RequestHeader) -> bool { #[async_trait] impl ProxyHttp for MyProxy { type CTX = MyCtx; - fn new_ctx(&self) -> Self::CTX { + fn new_ctx(&self, _session: &Session) -> Self::CTX { MyCtx { beta_user: false } } diff --git a/pingora-proxy/examples/gateway.rs b/pingora-proxy/examples/gateway.rs index 0bd53306..581e5386 100644 --- a/pingora-proxy/examples/gateway.rs +++ b/pingora-proxy/examples/gateway.rs @@ -36,7 +36,7 @@ pub struct MyGateway { #[async_trait] impl ProxyHttp for MyGateway { type CTX = (); - fn new_ctx(&self) -> Self::CTX {} + fn new_ctx(&self, _session: &Session) -> Self::CTX {} async fn request_filter(&self, session: &mut Session, _ctx: &mut Self::CTX) -> Result { if session.req_header().uri.path().starts_with("/login") diff --git a/pingora-proxy/examples/load_balancer.rs b/pingora-proxy/examples/load_balancer.rs index 614981d6..0d599ecb 100644 --- a/pingora-proxy/examples/load_balancer.rs +++ b/pingora-proxy/examples/load_balancer.rs @@ -30,7 +30,7 @@ pub struct LB(Arc>); #[async_trait] impl ProxyHttp for LB { type CTX = (); - fn new_ctx(&self) -> Self::CTX {} + fn new_ctx(&self, _session: &Session) -> Self::CTX {} async fn upstream_peer(&self, _session: &mut Session, _ctx: &mut ()) -> Result> { let upstream = self diff --git a/pingora-proxy/examples/modify_response.rs b/pingora-proxy/examples/modify_response.rs index 6730f718..bea3fa53 100644 --- a/pingora-proxy/examples/modify_response.rs +++ b/pingora-proxy/examples/modify_response.rs @@ -43,7 +43,7 @@ pub struct MyCtx { #[async_trait] impl ProxyHttp for Json2Yaml { type CTX = MyCtx; - fn new_ctx(&self) -> Self::CTX { + fn new_ctx(&self, _session: &Session) -> Self::CTX { MyCtx { buffer: vec![] } } diff --git a/pingora-proxy/examples/multi_lb.rs b/pingora-proxy/examples/multi_lb.rs index 1321c207..ab9cef30 100644 --- a/pingora-proxy/examples/multi_lb.rs +++ b/pingora-proxy/examples/multi_lb.rs @@ -31,7 +31,7 @@ struct Router { #[async_trait] impl ProxyHttp for Router { type CTX = (); - fn new_ctx(&self) {} + fn new_ctx(&self, _session: &Session) {} async fn upstream_peer(&self, session: &mut Session, _ctx: &mut ()) -> Result> { // determine LB cluster based on request uri diff --git a/pingora-proxy/src/lib.rs b/pingora-proxy/src/lib.rs index a02b23fc..0202b446 100644 --- a/pingora-proxy/src/lib.rs +++ b/pingora-proxy/src/lib.rs @@ -100,7 +100,7 @@ pub struct HttpProxy { } impl HttpProxy { - fn new(inner: SV, conf: Arc) -> Self { + pub fn new(inner: SV, conf: Arc) -> Self { HttpProxy { inner, client_upstream: Connector::new(Some(ConnectorOptions::from_server_conf(&conf))), @@ -691,8 +691,8 @@ where session.set_keepalive(None); session.subrequest_ctx.replace(sub_req_ctx); + let ctx = self.inner.new_ctx(&session); trace!("processing subrequest"); - let ctx = self.inner.new_ctx(); self.process_request(session, ctx).await; trace!("subrequest done"); } @@ -710,7 +710,6 @@ where shutdown: &ShutdownWatch, ) -> Option { let session = Box::new(session); - // TODO: keepalive pool, use stack let mut session = match self.handle_new_request(session).await { Some(downstream_session) => Session::new(downstream_session, &self.downstream_modules), @@ -725,7 +724,7 @@ where session.set_keepalive(Some(60)); } - let ctx = self.inner.new_ctx(); + let ctx = self.inner.new_ctx(&session); self.process_request(session, ctx).await } diff --git a/pingora-proxy/src/proxy_trait.rs b/pingora-proxy/src/proxy_trait.rs index 2d99567e..346d218a 100644 --- a/pingora-proxy/src/proxy_trait.rs +++ b/pingora-proxy/src/proxy_trait.rs @@ -28,7 +28,7 @@ pub trait ProxyHttp { type CTX; /// Define how the `ctx` should be created. - fn new_ctx(&self) -> Self::CTX; + fn new_ctx(&self, session: &Session) -> Self::CTX; /// Define where the proxy should send the request to. /// diff --git a/pingora-proxy/tests/utils/server_utils.rs b/pingora-proxy/tests/utils/server_utils.rs index ec1a9627..3375ecf7 100644 --- a/pingora-proxy/tests/utils/server_utils.rs +++ b/pingora-proxy/tests/utils/server_utils.rs @@ -28,6 +28,8 @@ use pingora_cache::{ }; use pingora_core::apps::{HttpServerApp, HttpServerOptions}; use pingora_core::modules::http::compression::ResponseCompression; +use pingora_core::protocols::http::client::HttpSession; +use pingora_core::protocols::http::ServerSession; use pingora_core::protocols::{l4::socket::SocketAddr, Digest}; use pingora_core::server::configuration::Opt; use pingora_core::services::Service; @@ -108,7 +110,7 @@ fn response_filter_common( #[async_trait] impl ProxyHttp for ExampleProxyHttps { type CTX = CTX; - fn new_ctx(&self) -> Self::CTX { + fn new_ctx(&self, session: &Session) -> Self::CTX { CTX::default() } @@ -205,7 +207,7 @@ pub struct ExampleProxyHttp {} #[async_trait] impl ProxyHttp for ExampleProxyHttp { type CTX = CTX; - fn new_ctx(&self) -> Self::CTX { + fn new_ctx(&self, session: &Session) -> Self::CTX { CTX::default() } @@ -338,7 +340,7 @@ pub struct ExampleProxyCache {} #[async_trait] impl ProxyHttp for ExampleProxyCache { type CTX = CacheCTX; - fn new_ctx(&self) -> Self::CTX { + fn new_ctx(&self, session: &Session) -> Self::CTX { CacheCTX { upstream_status: None, } From 2fd660aa7767c123f0b7b318636f522660730bc6 Mon Sep 17 00:00:00 2001 From: taikulawo Date: Tue, 16 Jul 2024 18:31:29 +0800 Subject: [PATCH 2/2] update --- pingora-proxy/examples/use_module.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pingora-proxy/examples/use_module.rs b/pingora-proxy/examples/use_module.rs index 4404768f..ca1af6c8 100644 --- a/pingora-proxy/examples/use_module.rs +++ b/pingora-proxy/examples/use_module.rs @@ -83,7 +83,7 @@ pub struct MyProxy; #[async_trait] impl ProxyHttp for MyProxy { type CTX = (); - fn new_ctx(&self) -> Self::CTX {} + fn new_ctx(&self, _session: &Session) -> Self::CTX {} // This function is only called once when the server starts fn init_downstream_modules(&self, modules: &mut HttpModules) {