diff --git a/pingora-proxy/examples/ctx.rs b/pingora-proxy/examples/ctx.rs index 3284b6f02..05e2bf4e8 100644 --- a/pingora-proxy/examples/ctx.rs +++ b/pingora-proxy/examples/ctx.rs @@ -17,7 +17,7 @@ use clap::Parser; use log::info; use std::sync::Mutex; -use pingora_core::{protocols::http::ServerSession, server::configuration::Opt}; +use pingora_core::server::configuration::Opt; use pingora_core::server::Server; use pingora_core::upstreams::peer::HttpPeer; use pingora_core::Result; @@ -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, session: &Box) -> 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 06b366cd4..581e53862 100644 --- a/pingora-proxy/examples/gateway.rs +++ b/pingora-proxy/examples/gateway.rs @@ -17,10 +17,10 @@ use clap::Parser; use log::info; use prometheus::register_int_counter; +use pingora_core::server::configuration::Opt; use pingora_core::server::Server; use pingora_core::upstreams::peer::HttpPeer; use pingora_core::Result; -use pingora_core::{protocols::http::ServerSession, server::configuration::Opt}; use pingora_http::ResponseHeader; use pingora_proxy::{ProxyHttp, Session}; @@ -36,7 +36,7 @@ pub struct MyGateway { #[async_trait] impl ProxyHttp for MyGateway { type CTX = (); - fn new_ctx(&self, _session: &Box) -> 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 13c1b6572..0d599ecb7 100644 --- a/pingora-proxy/examples/load_balancer.rs +++ b/pingora-proxy/examples/load_balancer.rs @@ -15,7 +15,7 @@ use async_trait::async_trait; use clap::Parser; use log::info; -use pingora_core::{protocols::http::ServerSession, services::background::background_service}; +use pingora_core::services::background::background_service; use std::{sync::Arc, time::Duration}; use pingora_core::server::configuration::Opt; @@ -30,7 +30,7 @@ pub struct LB(Arc>); #[async_trait] impl ProxyHttp for LB { type CTX = (); - fn new_ctx(&self, _session: &Box) -> 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 3d6355b3f..bea3fa535 100644 --- a/pingora-proxy/examples/modify_response.rs +++ b/pingora-proxy/examples/modify_response.rs @@ -18,10 +18,10 @@ use clap::Parser; use serde::{Deserialize, Serialize}; use std::net::ToSocketAddrs; +use pingora_core::server::configuration::Opt; use pingora_core::server::Server; use pingora_core::upstreams::peer::HttpPeer; use pingora_core::Result; -use pingora_core::{protocols::http::ServerSession, server::configuration::Opt}; use pingora_http::ResponseHeader; use pingora_proxy::{ProxyHttp, Session}; @@ -43,7 +43,7 @@ pub struct MyCtx { #[async_trait] impl ProxyHttp for Json2Yaml { type CTX = MyCtx; - fn new_ctx(&self, _session: &Box) -> 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 a8f5da5cc..ab9cef305 100644 --- a/pingora-proxy/examples/multi_lb.rs +++ b/pingora-proxy/examples/multi_lb.rs @@ -15,7 +15,7 @@ use async_trait::async_trait; use std::sync::Arc; -use pingora_core::{prelude::*, protocols::http::ServerSession, services::background::GenBackgroundService}; +use pingora_core::{prelude::*, services::background::GenBackgroundService}; use pingora_load_balancing::{ health_check::TcpHealthCheck, selection::{BackendIter, BackendSelection, RoundRobin}, @@ -31,7 +31,7 @@ struct Router { #[async_trait] impl ProxyHttp for Router { type CTX = (); - fn new_ctx(&self, _session: &Box) {} + 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 6e5731a0c..49f70918a 100644 --- a/pingora-proxy/src/lib.rs +++ b/pingora-proxy/src/lib.rs @@ -680,17 +680,17 @@ where sub_req_ctx: Box, ) { debug!("starting subrequest"); - let ctx = self.inner.new_ctx(&session); let mut session = match self.handle_new_request(session).await { Some(downstream_session) => Session::new(downstream_session, &self.downstream_modules), None => return, // bad request }; - + // no real downstream to keepalive, but it doesn't matter what is set here because at the end // of this fn the dummy connection will be dropped session.set_keepalive(None); - + session.subrequest_ctx.replace(sub_req_ctx); + let ctx = self.inner.new_ctx(&session); trace!("processing subrequest"); self.process_request(session, ctx).await; trace!("subrequest done"); @@ -709,13 +709,12 @@ where shutdown: &ShutdownWatch, ) -> Option { let session = Box::new(session); - let ctx = self.inner.new_ctx(&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), None => return None, // bad request }; - + if *shutdown.borrow() { // stop downstream from reusing if this service is shutting down soon session.set_keepalive(None); @@ -723,7 +722,8 @@ where // default 60s session.set_keepalive(Some(60)); } - + + 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 9feae715e..346d218a2 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, session: &Box) -> 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 c7977fca8..3375ecf7e 100644 --- a/pingora-proxy/tests/utils/server_utils.rs +++ b/pingora-proxy/tests/utils/server_utils.rs @@ -110,7 +110,7 @@ fn response_filter_common( #[async_trait] impl ProxyHttp for ExampleProxyHttps { type CTX = CTX; - fn new_ctx(&self, session: &Box) -> Self::CTX { + fn new_ctx(&self, session: &Session) -> Self::CTX { CTX::default() } @@ -207,7 +207,7 @@ pub struct ExampleProxyHttp {} #[async_trait] impl ProxyHttp for ExampleProxyHttp { type CTX = CTX; - fn new_ctx(&self, session: &Box) -> Self::CTX { + fn new_ctx(&self, session: &Session) -> Self::CTX { CTX::default() } @@ -340,7 +340,7 @@ pub struct ExampleProxyCache {} #[async_trait] impl ProxyHttp for ExampleProxyCache { type CTX = CacheCTX; - fn new_ctx(&self, session: &Box) -> Self::CTX { + fn new_ctx(&self, session: &Session) -> Self::CTX { CacheCTX { upstream_status: None, }