Skip to content

Commit

Permalink
refactor: reconsider http read header timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
junkurihara committed Jan 22, 2024
1 parent 5c5df68 commit 8081eb9
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
2 changes: 1 addition & 1 deletion rpxy-lib/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub const MAX_CONCURRENT_STREAMS: u32 = 64;
pub const CERTS_WATCH_DELAY_SECS: u32 = 60;
pub const LOAD_CERTS_ONLY_WHEN_UPDATED: bool = true;

pub const CONNECTION_TIMEOUT_SEC: u64 = 30; // timeout to serve a connection. this might limits the max length of response.
pub const CONNECTION_TIMEOUT_SEC: u64 = 60; // timeout to serve a connection, total time of receive request, serve, and send response. this might limits the max length of response.

// #[cfg(feature = "http3")]
// pub const H3_RESPONSE_BUF_SIZE: usize = 65_536; // 64KB
Expand Down
14 changes: 12 additions & 2 deletions rpxy-lib/src/forwarder/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ Please enable native-tls-backend or rustls-backend feature to enable TLS support
);
let executor = LocalExecutor::new(_globals.runtime_handle.clone());
let mut http = HttpConnector::new();
http.enforce_http(true);
http.set_reuse_address(true);
http.set_keepalive(Some(_globals.proxy_config.upstream_idle_timeout));
let inner = Client::builder(executor).build::<_, B>(http);
let inner_h2 = inner.clone();

Expand Down Expand Up @@ -216,8 +218,16 @@ where
#[cfg(not(feature = "rustls-backend-webpki"))]
info!("Native cert store with rustls is used for the connection to backend applications");

let connector = builder.https_or_http().enable_all_versions().build();
let connector_h2 = builder_h2.https_or_http().enable_http2().build();
let mut http = HttpConnector::new();
http.enforce_http(false);
http.set_reuse_address(true);
http.set_keepalive(Some(_globals.proxy_config.upstream_idle_timeout));

let connector = builder
.https_or_http()
.enable_all_versions()
.wrap_connector(http.clone());
let connector_h2 = builder_h2.https_or_http().enable_http2().wrap_connector(http);
let inner = Client::builder(LocalExecutor::new(_globals.runtime_handle.clone())).build::<_, B1>(connector);
let inner_h2 = Client::builder(LocalExecutor::new(_globals.runtime_handle.clone())).build::<_, B1>(connector_h2);

Expand Down
10 changes: 8 additions & 2 deletions rpxy-lib/src/proxy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,14 @@ pub(crate) fn connection_builder(globals: &Arc<Globals>) -> Arc<ConnectionBuilde
.pipeline_flush(true);
http_server
.http2()
.keep_alive_interval(Some(globals.proxy_config.proxy_idle_timeout))
.timer(TokioTimer)
.max_concurrent_streams(globals.proxy_config.max_concurrent_streams);

if globals.proxy_config.keepalive {
http_server
.http2()
.keep_alive_interval(Some(globals.proxy_config.proxy_idle_timeout))
.keep_alive_timeout(globals.proxy_config.proxy_idle_timeout + std::time::Duration::from_secs(1))
.timer(TokioTimer);
}
Arc::new(http_server)
}
3 changes: 1 addition & 2 deletions rpxy-lib/src/proxy/proxy_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,9 @@ where
let message_handler_clone = self.message_handler.clone();
let tls_enabled = self.tls_enabled;
let listening_on = self.listening_on;
let timeout_sec = Duration::from_secs(CONNECTION_TIMEOUT_SEC + 1); // just in case...
self.globals.runtime_handle.clone().spawn(async move {
timeout(
timeout_sec + Duration::from_secs(1), // just in case...
Duration::from_secs(CONNECTION_TIMEOUT_SEC) + Duration::from_secs(1), // just in case...
server_clone.serve_connection_with_upgrades(
stream,
service_fn(move |req: Request<Incoming>| {
Expand Down

0 comments on commit 8081eb9

Please sign in to comment.