Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add session on ProxyHttp#new_ctx function #329

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pingora-core/src/protocols/ssl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ impl ALPN {
}
}

pub(crate) fn from_wire_selected(raw: &[u8]) -> Option<Self> {
pub fn from_wire_selected(raw: &[u8]) -> Option<Self> {
match raw {
b"http/1.1" => Some(Self::H1),
b"h2" => Some(Self::H2),
Expand Down
2 changes: 1 addition & 1 deletion pingora-proxy/examples/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}

Expand Down
2 changes: 1 addition & 1 deletion pingora-proxy/examples/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> {
if session.req_header().uri.path().starts_with("/login")
Expand Down
2 changes: 1 addition & 1 deletion pingora-proxy/examples/load_balancer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub struct LB(Arc<LoadBalancer<RoundRobin>>);
#[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<Box<HttpPeer>> {
let upstream = self
Expand Down
2 changes: 1 addition & 1 deletion pingora-proxy/examples/modify_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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![] }
}

Expand Down
2 changes: 1 addition & 1 deletion pingora-proxy/examples/multi_lb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Box<HttpPeer>> {
// determine LB cluster based on request uri
Expand Down
2 changes: 1 addition & 1 deletion pingora-proxy/examples/use_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 3 additions & 4 deletions pingora-proxy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ pub struct HttpProxy<SV> {
}

impl<SV> HttpProxy<SV> {
fn new(inner: SV, conf: Arc<ServerConf>) -> Self {
pub fn new(inner: SV, conf: Arc<ServerConf>) -> Self {
HttpProxy {
inner,
client_upstream: Connector::new(Some(ConnectorOptions::from_server_conf(&conf))),
Expand Down Expand Up @@ -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");
}
Expand All @@ -710,7 +710,6 @@ where
shutdown: &ShutdownWatch,
) -> Option<Stream> {
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),
Expand All @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion pingora-proxy/src/proxy_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down
8 changes: 5 additions & 3 deletions pingora-proxy/tests/utils/server_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
}

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

Expand Down Expand Up @@ -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,
}
Expand Down