Skip to content

Commit

Permalink
perf(dgw): keep HTTP connections open for 10 minutes (#863)
Browse files Browse the repository at this point in the history
Most browsers will keep HTTP connections open to increase throughput
when performing subsequent transactions.
For simplicity, we don’t distinguish between idle and non-idle
connections.
  • Loading branch information
CBenoit authored May 16, 2024
1 parent 717d53e commit 245e2cf
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion devolutions-gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ tokio-rustls = { version = "0.24", features = ["dangerous_configuration", "tls12
reqwest = { version = "0.12", default-features = false, features = ["rustls-tls-native-roots", "json"] } # TODO: directly use hyper in subscriber module
futures = "0.3"
async-trait = "0.1"
tower = "0.4"
tower = { version = "0.4", features = ["timeout"] }
ngrok = "0.13"

# HTTP
Expand Down
10 changes: 9 additions & 1 deletion devolutions-gateway/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ impl DgwState {
}

pub fn make_http_service(state: DgwState) -> axum::Router<()> {
use axum::error_handling::HandleErrorLayer;
use std::time::Duration;
use tower::timeout::TimeoutLayer;
use tower::ServiceBuilder;

trace!("Make http service");
Expand All @@ -101,6 +104,11 @@ pub fn make_http_service(state: DgwState) -> axum::Router<()> {
.layer(axum::middleware::from_fn_with_state(
state,
middleware::auth::auth_middleware,
)),
))
// This middleware goes above `TimeoutLayer` because it will receive errors returned by `TimeoutLayer`.
.layer(HandleErrorLayer::new(|_: axum::BoxError| async {
hyper::StatusCode::REQUEST_TIMEOUT
}))
.layer(TimeoutLayer::new(Duration::from_secs(15))),
)
}
10 changes: 5 additions & 5 deletions devolutions-gateway/src/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::generic_client::GenericClient;
use crate::utils::url_to_socket_addr;
use crate::DgwState;

const HTTP_REQUEST_TIMEOUT: tokio::time::Duration = tokio::time::Duration::from_secs(15);
const HTTP_CONNECTION_MAX_DURATION: tokio::time::Duration = tokio::time::Duration::from_secs(10 * 60);

#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[derive(Debug, Clone, Serialize)]
Expand Down Expand Up @@ -170,12 +170,12 @@ async fn run_http_listener(listener: TcpListener, state: DgwState) -> anyhow::Re
Ok((stream, peer_addr)) => {
let state = state.clone();

let fut = tokio::time::timeout(HTTP_REQUEST_TIMEOUT, async move {
let fut = tokio::time::timeout(HTTP_CONNECTION_MAX_DURATION, async move {
if let Err(e) = handle_http_peer(stream, state, peer_addr).await {
error!(error = format!("{e:#}"), "handle_http_peer failed");
}
})
.inspect_err(|error| warn!(%error, "Request timed out"))
.inspect_err(|error| debug!(%error, "Drop long-lived HTTP connection"))
.instrument(info_span!("http", client = %peer_addr));

ChildTask::spawn(fut).detach();
Expand All @@ -198,12 +198,12 @@ async fn run_https_listener(listener: TcpListener, state: DgwState) -> anyhow::R
let tls_acceptor = tls_conf.acceptor.clone();
let state = state.clone();

let fut = tokio::time::timeout(HTTP_REQUEST_TIMEOUT, async move {
let fut = tokio::time::timeout(HTTP_CONNECTION_MAX_DURATION, async move {
if let Err(e) = handle_https_peer(stream, tls_acceptor, state, peer_addr).await {
error!(error = format!("{e:#}"), "handle_https_peer failed");
}
})
.inspect_err(|error| warn!(%error, "Request timed out"))
.inspect_err(|error| debug!(%error, "Drop long-lived HTTP connection"))
.instrument(info_span!("https", client = %peer_addr));

ChildTask::spawn(fut).detach();
Expand Down

0 comments on commit 245e2cf

Please sign in to comment.