Skip to content

Commit

Permalink
fix: keep connections open
Browse files Browse the repository at this point in the history
  • Loading branch information
chris13524 committed May 20, 2024
1 parent b0eb7d5 commit acb2a4d
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,20 @@ pub struct ClientConfig {
pub request_timeout_secs: Option<u64>,
/// The timeout for idle sockets being kept alive
pub pool_idle_timeout_secs: Option<u64>,
pub http2_keep_alive_interval_secs: Option<u64>,
pub http2_keep_alive_while_idle: bool,
}

impl Default for ClientConfig {
fn default() -> Self {
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,
}
}
}
Expand Down Expand Up @@ -130,13 +136,17 @@ impl ClientBuilder {
endpoint,
request_timeout_secs,
pool_idle_timeout_secs,
http2_keep_alive_interval_secs,
http2_keep_alive_while_idle,
},
signer,
connector,
} = self;
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 {
Expand Down

0 comments on commit acb2a4d

Please sign in to comment.