diff --git a/src/service/http.rs b/src/service/http.rs index b4d81ba..4676440 100644 --- a/src/service/http.rs +++ b/src/service/http.rs @@ -548,6 +548,7 @@ impl Service { async fn handle_request( &self, + proxy_client: sync::Arc, client_ip: net::IpAddr, req: Request, req_is_https: bool, @@ -614,6 +615,7 @@ impl Service { let http_url = config.http_url.as_ref().unwrap(); return service::http::proxy::serve_http_request( + proxy_client.as_ref(), http_url.original_url.as_str(), &config.http_request_headers.0, client_ip, @@ -667,6 +669,7 @@ fn strip_port(host: &str) -> &str { struct HyperServiceImpl { service: sync::Arc, + proxy_client: sync::Arc, client_ip: net::IpAddr, is_https: bool, } @@ -675,6 +678,7 @@ impl HyperServiceImpl { pub fn new(service: sync::Arc, client_ip: net::IpAddr, is_https: bool) -> Self { Self { service, + proxy_client: service::http::proxy::new_client().into(), client_ip, is_https, } @@ -688,8 +692,13 @@ impl hyper::service::Service for HyperServiceImpl { fn call(&self, req: Request) -> Self::Future { let service = self.service.clone(); + let proxy_client = self.proxy_client.clone(); let client_ip = self.client_ip; let is_https = self.is_https; - Box::pin(async move { Ok(service.handle_request(client_ip, req, is_https).await) }) + Box::pin(async move { + Ok(service + .handle_request(proxy_client, client_ip, req, is_https) + .await) + }) } } diff --git a/src/service/http/proxy.rs b/src/service/http/proxy.rs index 96de1d8..258991c 100644 --- a/src/service/http/proxy.rs +++ b/src/service/http/proxy.rs @@ -6,18 +6,17 @@ use hyper_reverse_proxy::ReverseProxy; use hyper_util::client::legacy::connect::HttpConnector; use hyper_util::rt::TokioExecutor; -fn proxy_client() -> &'static ReverseProxy { - use std::sync::OnceLock; - static PROXY_CLIENT: OnceLock> = OnceLock::new(); - PROXY_CLIENT.get_or_init(|| { - ReverseProxy::new( - hyper_util::client::legacy::Builder::new(TokioExecutor::new()) - .build::<_, Incoming>(HttpConnector::new()), - ) - }) +pub type Client = ReverseProxy; + +pub fn new_client() -> Client { + ReverseProxy::new( + hyper_util::client::legacy::Builder::new(TokioExecutor::new()) + .build::<_, Incoming>(HttpConnector::new()), + ) } pub async fn serve_http_request( + proxy_client: &Client, proxy_addr: &str, headers: &http::header::HeaderMap, client_ip: net::IpAddr, @@ -40,7 +39,7 @@ pub async fn serve_http_request( ); } - match proxy_client().call(client_ip, proxy_addr, req).await { + match proxy_client.call(client_ip, proxy_addr, req).await { Ok(res) => Ok(res), // ProxyError doesn't actually implement Error :facepalm: so we have to format the error // manually