From acb2a4d9d846a20f24c3271fb304d1da063ca9c4 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Mon, 20 May 2024 08:05:34 -0400 Subject: [PATCH] fix: keep connections open --- src/client.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/client.rs b/src/client.rs index 9da3c8d..60a5062 100644 --- a/src/client.rs +++ b/src/client.rs @@ -69,6 +69,8 @@ pub struct ClientConfig { pub request_timeout_secs: Option, /// The timeout for idle sockets being kept alive pub pool_idle_timeout_secs: Option, + pub http2_keep_alive_interval_secs: Option, + pub http2_keep_alive_while_idle: bool, } impl Default for ClientConfig { @@ -76,7 +78,11 @@ impl Default for ClientConfig { Self { endpoint: Endpoint::Production, request_timeout_secs: Some(DEFAULT_REQUEST_TIMEOUT_SECS), - pool_idle_timeout_secs: Some(600), + pool_idle_timeout_secs: None, + // Send HTTP/2 PING every 1 hour as per: https://developer.apple.com/documentation/usernotifications/sending-notification-requests-to-apns#Follow-best-practices-while-sending-push-notifications-with-APNs + // Reuse a connection as long as possible. In most cases, you can reuse a connection for many hours to days. If your connection is mostly idle, you may send a HTTP2 PING frame after an hour of inactivity. Reusing a connection often results in less bandwidth and CPU consumption. + http2_keep_alive_interval_secs: Some(60 * 60), + http2_keep_alive_while_idle: true, } } } @@ -130,6 +136,8 @@ impl ClientBuilder { endpoint, request_timeout_secs, pool_idle_timeout_secs, + http2_keep_alive_interval_secs, + http2_keep_alive_while_idle, }, signer, connector, @@ -137,6 +145,8 @@ impl ClientBuilder { let http_client = HttpClient::builder(TokioExecutor::new()) .pool_idle_timeout(pool_idle_timeout_secs.map(Duration::from_secs)) .http2_only(true) + .http2_keep_alive_interval(http2_keep_alive_interval_secs.map(Duration::from_secs)) + .http2_keep_alive_while_idle(http2_keep_alive_while_idle) .build(connector.unwrap_or_else(default_connector)); Client {