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

fix: keep connections open #87

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found a panic during testing. Please include the timer:

Suggested change
.http2_keep_alive_while_idle(http2_keep_alive_while_idle)
.http2_keep_alive_while_idle(http2_keep_alive_while_idle)
.timer(TokioTimer::new())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested with this change and the connection resets have stopped. Thanks again for this change

.build(connector.unwrap_or_else(default_connector));

Client {
Expand Down
Loading