Skip to content

Commit

Permalink
tproxy: Config for overall timeout for a connection
Browse files Browse the repository at this point in the history
  • Loading branch information
kvinwang committed Dec 15, 2024
1 parent 8456790 commit 075086d
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 11 deletions.
2 changes: 2 additions & 0 deletions tproxy/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ pub struct Timeouts {
pub write: Duration,
#[serde(with = "serde_duration")]
pub shutdown: Duration,
#[serde(with = "serde_duration")]
pub total: Duration,
}

#[derive(Debug, Clone, Deserialize)]
Expand Down
28 changes: 19 additions & 9 deletions tproxy/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,20 +153,30 @@ pub async fn run(config: &ProxyConfig, app_state: AppState) -> Result<()> {
loop {
match listener.accept().await {
Ok((inbound, addr)) => {
info!("new connection from {addr}");
info!(%addr, "new connection received");
let app_state = app_state.clone();
let dotted_base_domain = dotted_base_domain.clone();
let tls_terminate_proxy = tls_terminate_proxy.clone();
tokio::spawn(async move {
if let Err(e) = handle_connection(
inbound,
app_state,
&dotted_base_domain,
tls_terminate_proxy,
let timeouts = &app_state.config.proxy.timeouts;
let result = timeout(
timeouts.total,
handle_connection(
inbound,
app_state,
&dotted_base_domain,
tls_terminate_proxy,
),
)
.await
{
error!("connection error: {e:?}");
.await;
match result {
Ok(Ok(_)) => {}
Ok(Err(e)) => {
error!("connection error: {e:?}");
}
Err(_) => {
info!(%addr, "connection kept too long");
}
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion tproxy/src/proxy/tls_passthough.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub(crate) async fn proxy_to_app(
TcpStream::connect((target_ip, port)),
)
.await
.context("connection timeout")?
.context("connecting timeout")?
.context("failed to connect to tapp")?;
outbound
.write_all(&buffer)
Expand Down
2 changes: 1 addition & 1 deletion tproxy/src/proxy/tls_terminate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl TlsTerminateProxy {
TcpStream::connect((host.ip, port)),
)
.await
.map_err(|_| anyhow::anyhow!("connection timeout"))?
.map_err(|_| anyhow::anyhow!("connecting timeout"))?
.context("failed to connect to app")?;
bridge(
IgnoreUnexpectedEofStream::new(tls_stream),
Expand Down
2 changes: 2 additions & 0 deletions tproxy/tproxy.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ idle = "10m"
write = "5s"
# Timeout for shutting down a connection.
shutdown = "5s"
# Timeout for total connection duration.
total = "5h"

[core.recycle]
enabled = true
Expand Down

0 comments on commit 075086d

Please sign in to comment.