Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(dgw): keep HTTP connections open for 10 minutes #863

Merged
merged 1 commit into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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