diff --git a/clients/rust/README.md b/clients/rust/README.md index 1c49c9a7..b13788eb 100644 --- a/clients/rust/README.md +++ b/clients/rust/README.md @@ -190,6 +190,21 @@ expected payload surfaces as `Error::MalformedReply`. TLS note: `ClientOptions::with_ca_file` is read asynchronously (`tokio::fs`) during `connect`, so connecting no longer blocks the executor thread. +### HTTP/2 keepalive + +The channel sends an HTTP/2 PING every 30 s, waits 10 s for the +acknowledgement, and pings even while no call is active +(`http2_keep_alive_interval` / `keep_alive_timeout` / +`keep_alive_while_idle(true)`). A half-open connection (firewall idle-out, host +power loss) therefore fails a deadline-less `events()` stream with a transport +error within about 40 s instead of hanging until the gateway's detach grace and +replay retention have expired. Tune with +`ClientOptions::with_keepalive_interval`, `with_keepalive_timeout` and +`with_keepalive_while_idle`, or turn it off with `without_keepalive`. Keep the +interval at or above the gateway's permitted client ping interval +(`MxGateway:*` keepalive policy, 30 s by default); pinging faster makes the +gateway send GOAWAY (`too_many_pings`). + ## Event Streaming And Reconnect-Replay Gaps `session.events()` / `session.events_after(after_worker_sequence)` (and the diff --git a/clients/rust/src/client.rs b/clients/rust/src/client.rs index f8672e43..9bc51b6c 100644 --- a/clients/rust/src/client.rs +++ b/clients/rust/src/client.rs @@ -21,7 +21,7 @@ use crate::generated::mxaccess_gateway::v1::{ OpenSessionReply, OpenSessionRequest, QueryActiveAlarmsRequest, ReplayGap, StreamAlarmsRequest, StreamEventsRequest, }; -use crate::options::{build_tls_config, ClientOptions}; +use crate::options::{apply_transport_options, build_tls_config, ClientOptions}; use crate::session::Session; /// Generated gateway client wrapped in the auth interceptor that @@ -185,7 +185,7 @@ impl GatewayClient { detail: source.to_string(), } })?; - endpoint = endpoint.connect_timeout(options.connect_timeout()); + endpoint = apply_transport_options(endpoint, &options); if let Some(tls) = build_tls_config(&options).await? { endpoint = endpoint.tls_config(tls)?; diff --git a/clients/rust/src/galaxy.rs b/clients/rust/src/galaxy.rs index 794d4b26..123496e4 100644 --- a/clients/rust/src/galaxy.rs +++ b/clients/rust/src/galaxy.rs @@ -22,7 +22,7 @@ use crate::generated::galaxy_repository::v1::{ DiscoverHierarchyRequest, GalaxyObject, GetLastDeployTimeRequest, TestConnectionRequest, WatchDeployEventsRequest, }; -use crate::options::{build_tls_config, ClientOptions}; +use crate::options::{apply_transport_options, build_tls_config, ClientOptions}; const DISCOVER_HIERARCHY_PAGE_SIZE: i32 = 5000; const BROWSE_CHILDREN_PAGE_SIZE: i32 = 500; @@ -180,7 +180,7 @@ impl GalaxyClient { detail: source.to_string(), } })?; - endpoint = endpoint.connect_timeout(options.connect_timeout()); + endpoint = apply_transport_options(endpoint, &options); if let Some(tls) = build_tls_config(&options).await? { endpoint = endpoint.tls_config(tls)?; diff --git a/clients/rust/src/options.rs b/clients/rust/src/options.rs index 7ffb9fde..b632e2cf 100644 --- a/clients/rust/src/options.rs +++ b/clients/rust/src/options.rs @@ -13,10 +13,20 @@ use crate::error::Error; const DEFAULT_MAX_GRPC_MESSAGE_BYTES: usize = 16 * 1024 * 1024; +/// Default HTTP/2 keepalive ping interval. Matches the gateway's permitted +/// client ping rate (30 s); pinging faster makes Kestrel send GOAWAY +/// (`too_many_pings`). +pub const DEFAULT_KEEPALIVE_INTERVAL: Duration = Duration::from_secs(30); + +/// Default time to wait for a keepalive ping acknowledgement before the +/// connection is treated as dead. +pub const DEFAULT_KEEPALIVE_TIMEOUT: Duration = Duration::from_secs(10); + /// Configuration for connecting to a gateway endpoint. /// /// Defaults are 10s connect timeout, 30s call timeout, no streaming timeout, -/// and plaintext (h2c) transport. Set [`ClientOptions::with_plaintext`] to +/// HTTP/2 keepalive (30s ping, 10s timeout, also while no call is active), and +/// plaintext (h2c) transport. Set [`ClientOptions::with_plaintext`] to /// `false` and supply [`ClientOptions::with_ca_file`] / a server-name override /// for TLS deployments. #[derive(Clone)] @@ -31,6 +41,9 @@ pub struct ClientOptions { call_timeout: Duration, stream_timeout: Option, max_grpc_message_bytes: usize, + keepalive_interval: Option, + keepalive_timeout: Duration, + keepalive_while_idle: bool, } impl ClientOptions { @@ -48,6 +61,9 @@ impl ClientOptions { call_timeout: Duration::from_secs(30), stream_timeout: None, max_grpc_message_bytes: DEFAULT_MAX_GRPC_MESSAGE_BYTES, + keepalive_interval: Some(DEFAULT_KEEPALIVE_INTERVAL), + keepalive_timeout: DEFAULT_KEEPALIVE_TIMEOUT, + keepalive_while_idle: true, } } @@ -128,6 +144,52 @@ impl ClientOptions { self } + /// HTTP/2 keepalive ping interval (default 30 s). Keepalive detects a + /// half-open connection (firewall idle-out, host power loss) so deadline-less + /// streams fail with a transport error instead of hanging forever. Keep this + /// at or above the gateway's permitted client ping interval + /// (`MxGateway:*` keepalive policy, 30 s by default) or the gateway answers + /// with GOAWAY. + pub fn with_keepalive_interval(mut self, interval: Duration) -> Self { + self.keepalive_interval = Some(interval); + self + } + + /// Disable HTTP/2 keepalive pings entirely. + pub fn without_keepalive(mut self) -> Self { + self.keepalive_interval = None; + self + } + + /// Time to wait for a keepalive ping acknowledgement (default 10 s) before + /// the connection is closed. + pub fn with_keepalive_timeout(mut self, timeout: Duration) -> Self { + self.keepalive_timeout = timeout; + self + } + + /// Whether keepalive pings are sent while no call is active (default + /// `true`). Required so an idle channel between calls is still probed. + pub fn with_keepalive_while_idle(mut self, while_idle: bool) -> Self { + self.keepalive_while_idle = while_idle; + self + } + + /// Configured keepalive ping interval; `None` when keepalive is disabled. + pub fn keepalive_interval(&self) -> Option { + self.keepalive_interval + } + + /// Configured keepalive acknowledgement timeout. + pub fn keepalive_timeout(&self) -> Duration { + self.keepalive_timeout + } + + /// Whether keepalive pings are sent while no call is active. + pub fn keepalive_while_idle(&self) -> bool { + self.keepalive_while_idle + } + /// Configured endpoint URL. pub fn endpoint(&self) -> &str { &self.endpoint @@ -180,6 +242,23 @@ impl ClientOptions { } } +/// Apply the connect timeout and HTTP/2 keepalive policy from `options` to an +/// endpoint. Shared by the gateway and Galaxy clients so both channels probe +/// half-open connections the same way. +pub(crate) fn apply_transport_options( + endpoint: tonic::transport::Endpoint, + options: &ClientOptions, +) -> tonic::transport::Endpoint { + let mut endpoint = endpoint.connect_timeout(options.connect_timeout()); + if let Some(interval) = options.keepalive_interval() { + endpoint = endpoint + .http2_keep_alive_interval(interval) + .keep_alive_timeout(options.keepalive_timeout()) + .keep_alive_while_idle(options.keepalive_while_idle()); + } + endpoint +} + /// Where the TLS handshake gets its trust anchors for a given set of options. /// Computed by [`tls_trust_decision`] and applied by [`build_tls_config`]; /// split out so the trust posture is unit-testable without a live handshake. @@ -316,17 +395,43 @@ impl fmt::Debug for ClientOptions { .field("call_timeout", &self.call_timeout) .field("stream_timeout", &self.stream_timeout) .field("max_grpc_message_bytes", &self.max_grpc_message_bytes) + .field("keepalive_interval", &self.keepalive_interval) + .field("keepalive_timeout", &self.keepalive_timeout) + .field("keepalive_while_idle", &self.keepalive_while_idle) .finish() } } #[cfg(test)] mod tests { + use std::time::Duration; + use super::ClientOptions; use crate::auth::ApiKey; use super::{build_tls_config, tls_trust_decision, TlsTrustDecision}; + #[test] + fn keepalive_defaults_match_the_gateway_policy() { + let options = ClientOptions::new("http://localhost:5000"); + + assert_eq!(options.keepalive_interval(), Some(Duration::from_secs(30))); + assert_eq!(options.keepalive_timeout(), Duration::from_secs(10)); + assert!(options.keepalive_while_idle()); + } + + #[test] + fn keepalive_is_configurable_and_can_be_disabled() { + let options = ClientOptions::new("http://localhost:5000") + .with_keepalive_interval(Duration::from_secs(45)) + .with_keepalive_timeout(Duration::from_secs(5)) + .with_keepalive_while_idle(false); + assert_eq!(options.keepalive_interval(), Some(Duration::from_secs(45))); + assert_eq!(options.keepalive_timeout(), Duration::from_secs(5)); + assert!(!options.keepalive_while_idle()); + assert_eq!(options.without_keepalive().keepalive_interval(), None); + } + #[test] fn debug_redacts_api_key() { let options =