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

tproxy: Config for overall timeout for a connection #57

Merged
merged 1 commit into from
Dec 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: 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
Loading