Skip to content

Commit

Permalink
Drop http client after every request.
Browse files Browse the repository at this point in the history
This also effectively drops the connection pool, which we don't have a need for.
  • Loading branch information
denschub committed Mar 25, 2024
1 parent 2ff0c92 commit 91f1dcb
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ use std::time::Duration;
use axum::{http::HeaderValue, response::IntoResponse};
use http_body_util::Empty;
use hyper::{body::Bytes, header, HeaderMap, Method, Request, Response};
use hyper_util::{
client::legacy::{connect::HttpConnector, Client},
rt::TokioExecutor,
};
use hyper_util::{client::legacy::Client, rt::TokioExecutor};

use crate::{errors::ProxyError, header_wrangler};

Expand All @@ -17,7 +14,6 @@ use crate::{errors::ProxyError, header_wrangler};
pub struct Proxy {
via_header: String,
upstream_timeout: usize,
http_client: Client<hyper_rustls::HttpsConnector<HttpConnector>, Empty<Bytes>>,
}

impl Proxy {
Expand All @@ -27,19 +23,9 @@ impl Proxy {
/// This will internally also create the hyper HttpsConnector and hyper
/// Client, which will be used throughout the life of this Proxy.
pub fn new(via_header: &str, upstream_timeout: usize) -> Self {
let https = hyper_rustls::HttpsConnectorBuilder::new()
.with_native_roots()
.expect("native roots to be there")
.https_or_http()
.enable_http1()
.enable_http2()
.build();
let http_client = Client::builder(TokioExecutor::new()).build::<_, Empty<Bytes>>(https);

Self {
via_header: via_header.to_owned(),
upstream_timeout,
http_client,
}
}

Expand All @@ -54,6 +40,15 @@ impl Proxy {
headers: &HeaderMap,
target: &str,
) -> Result<Response<axum::body::Body>, ProxyError> {
let https = hyper_rustls::HttpsConnectorBuilder::new()
.with_native_roots()
.expect("native roots to be there")
.https_or_http()
.enable_http1()
.enable_http2()
.build();
let http_client = Client::builder(TokioExecutor::new()).build::<_, Empty<Bytes>>(https);

let mut req = Request::builder()
.method(method)
.uri(target)
Expand All @@ -64,7 +59,7 @@ impl Proxy {

header_wrangler::assign_filtered_request_headers(headers, req.headers_mut());

let request_future = self.http_client.request(req);
let request_future = http_client.request(req);
let mut res = tokio::time::timeout(
Duration::from_secs(self.upstream_timeout as u64),
request_future,
Expand Down

0 comments on commit 91f1dcb

Please sign in to comment.