Kill connections lasting more than 24h

This commit is contained in:
Alex 2022-01-24 19:49:14 +01:00
parent 21ea26bbff
commit 10d13b194b
No known key found for this signature in database
GPG key ID: EDABF9711E244EB1

View file

@ -25,6 +25,9 @@ use crate::cert_store::{CertStore, StoreResolver};
use crate::proxy_config::ProxyConfig; use crate::proxy_config::ProxyConfig;
use crate::reverse_proxy; use crate::reverse_proxy;
const PROXY_TIMEOUT: Duration = Duration::from_secs(60);
const MAX_CONNECTION_LIFETIME: Duration = Duration::from_secs(24 * 3600);
pub struct HttpsConfig { pub struct HttpsConfig {
pub bind_addr: SocketAddr, pub bind_addr: SocketAddr,
pub enable_compression: bool, pub enable_compression: bool,
@ -76,10 +79,12 @@ pub async fn serve_https(
handle_outer(remote_addr, req, https_config, proxy_config) handle_outer(remote_addr, req, https_config, proxy_config)
}), }),
); );
tokio::pin!(http_conn); let timeout = tokio::time::sleep(MAX_CONNECTION_LIFETIME);
tokio::pin!(http_conn, timeout);
let http_result = loop { let http_result = loop {
select! ( select! (
r = &mut http_conn => break r, r = &mut http_conn => break r.map_err(Into::into),
_ = &mut timeout => break Err(anyhow!("Connection lived more than 24h, killing it.")),
_ = must_exit_2.changed() => { _ = must_exit_2.changed() => {
if *must_exit_2.borrow() { if *must_exit_2.borrow() {
http_conn.as_mut().graceful_shutdown(); http_conn.as_mut().graceful_shutdown();
@ -97,6 +102,8 @@ pub async fn serve_https(
connections.push(conn); connections.push(conn);
} }
drop(tcp);
info!("HTTPS server shutting down, draining remaining connections..."); info!("HTTPS server shutting down, draining remaining connections...");
while !connections.is_empty() { while !connections.is_empty() {
let _ = connections.next().await; let _ = connections.next().await;
@ -227,7 +234,7 @@ async fn handle_timeout_and_error(
.unwrap(), .unwrap(),
} }
} }
_ = tokio::time::sleep(Duration::from_secs(60)) => { _ = tokio::time::sleep(PROXY_TIMEOUT) => {
Response::builder() Response::builder()
.status(StatusCode::BAD_GATEWAY) .status(StatusCode::BAD_GATEWAY)
.body(Body::from("Proxy timeout")) .body(Body::from("Proxy timeout"))