Add forward_timeout to LP client config

Add forward_timeout (30s default) to LpConfig and wrap send_forward_packet's
connect_send_receive call with tokio::time::timeout, matching the pattern
used by register() with registration_timeout.

This prevents indefinite hangs when forwarding packets through entry gateway.
This commit is contained in:
durch
2025-12-16 20:22:29 +01:00
committed by Jędrzej Stuczyński
parent bb76ff82bd
commit 1386a49a99
2 changed files with 27 additions and 8 deletions
@@ -838,15 +838,24 @@ impl LpRegistrationClient {
.ok()
.and_then(|s| s.outer_aead_key());
// 5. Send and receive on fresh connection
let response_packet = Self::connect_send_receive(
self.gateway_lp_address,
&forward_packet,
send_key.as_ref(),
recv_key.as_ref(),
&self.config,
// 5. Send and receive on fresh connection with timeout
let response_packet = tokio::time::timeout(
self.config.forward_timeout,
Self::connect_send_receive(
self.gateway_lp_address,
&forward_packet,
send_key.as_ref(),
recv_key.as_ref(),
&self.config,
),
)
.await?;
.await
.map_err(|_| {
LpClientError::Transport(format!(
"Forward packet timeout after {:?}",
self.config.forward_timeout
))
})??;
tracing::trace!("Received response packet from entry gateway");
// 6. Decrypt via state machine
@@ -16,6 +16,7 @@ use std::time::Duration;
/// - `connect_timeout`: 10 seconds - reasonable for real network conditions
/// - `handshake_timeout`: 15 seconds - allows for Noise handshake round-trips
/// - `registration_timeout`: 30 seconds - includes credential verification and response
/// - `forward_timeout`: 30 seconds - forward packet send/receive to exit gateway
/// - `tcp_nodelay`: true - lower latency for small registration messages
/// - `tcp_keepalive`: None - not needed for short-lived registration connections
///
@@ -46,6 +47,13 @@ pub struct LpConfig {
/// Default: 30 seconds.
pub registration_timeout: Duration,
/// Forward packet send/receive timeout.
///
/// Maximum time to wait for forward packet send + response receive via entry gateway.
/// Covers the entire round-trip through entry to exit gateway and back.
/// Default: 30 seconds.
pub forward_timeout: Duration,
/// Enable TCP_NODELAY (disable Nagle's algorithm) (nym-104).
///
/// When true, disables Nagle's algorithm for lower latency.
@@ -68,6 +76,7 @@ impl Default for LpConfig {
connect_timeout: Duration::from_secs(10),
handshake_timeout: Duration::from_secs(15),
registration_timeout: Duration::from_secs(30),
forward_timeout: Duration::from_secs(30),
// nym-104: Optimized for registration-only protocol
tcp_nodelay: true, // Lower latency for small messages
@@ -87,6 +96,7 @@ mod tests {
assert_eq!(config.connect_timeout, Duration::from_secs(10));
assert_eq!(config.handshake_timeout, Duration::from_secs(15));
assert_eq!(config.registration_timeout, Duration::from_secs(30));
assert_eq!(config.forward_timeout, Duration::from_secs(30));
assert!(config.tcp_nodelay);
assert_eq!(config.tcp_keepalive, None);
}