Skip to content

Commit

Permalink
Expose hyper HTTP2 keep-alive config.
Browse files Browse the repository at this point in the history
  • Loading branch information
hson authored and seanmonstar committed May 17, 2022
1 parent 2a6e012 commit 0b9b499
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/async_impl/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ struct Config {
http2_initial_connection_window_size: Option<u32>,
http2_adaptive_window: bool,
http2_max_frame_size: Option<u32>,
http2_keep_alive_interval: Option<Duration>,
http2_keep_alive_timeout: Option<Duration>,
http2_keep_alive_while_idle: bool,
local_address: Option<IpAddr>,
nodelay: bool,
#[cfg(feature = "cookies")]
Expand Down Expand Up @@ -175,6 +178,9 @@ impl ClientBuilder {
http2_initial_connection_window_size: None,
http2_adaptive_window: false,
http2_max_frame_size: None,
http2_keep_alive_interval: None,
http2_keep_alive_timeout: None,
http2_keep_alive_while_idle: false,
local_address: None,
nodelay: true,
trust_dns: cfg!(feature = "trust-dns"),
Expand Down Expand Up @@ -476,6 +482,15 @@ impl ClientBuilder {
if let Some(http2_max_frame_size) = config.http2_max_frame_size {
builder.http2_max_frame_size(http2_max_frame_size);
}
if let Some(http2_keep_alive_interval) = config.http2_keep_alive_interval {
builder.http2_keep_alive_interval(http2_keep_alive_interval);
}
if let Some(http2_keep_alive_timeout) = config.http2_keep_alive_timeout {
builder.http2_keep_alive_timeout(http2_keep_alive_timeout);
}
if config.http2_keep_alive_while_idle {
builder.http2_keep_alive_while_idle(true);
}

builder.pool_idle_timeout(config.pool_idle_timeout);
builder.pool_max_idle_per_host(config.pool_max_idle_per_host);
Expand Down Expand Up @@ -935,6 +950,36 @@ impl ClientBuilder {
self
}

/// Sets an interval for HTTP2 Ping frames should be sent to keep a connection alive.
///
/// Pass `None` to disable HTTP2 keep-alive.
/// Default is currently disabled.
pub fn http2_keep_alive_interval(mut self, interval: impl Into<Option<Duration>>) -> ClientBuilder {
self.config.http2_keep_alive_interval = interval.into();
self
}

/// Sets a timeout for receiving an acknowledgement of the keep-alive ping.
///
/// If the ping is not acknowledged within the timeout, the connection will be closed.
/// Does nothing if `http2_keep_alive_interval` is disabled.
/// Default is currently disabled.
pub fn http2_keep_alive_timeout(mut self, timeout: Duration) -> ClientBuilder {
self.config.http2_keep_alive_timeout = Some(timeout);
self
}

/// Sets whether HTTP2 keep-alive should apply while the connection is idle.
///
/// If disabled, keep-alive pings are only sent while there are open request/responses streams.
/// If enabled, pings are also sent when no streams are active.
/// Does nothing if `http2_keep_alive_interval` is disabled.
/// Default is `false`.
pub fn http2_keep_alive_while_idle(mut self, enabled: bool) -> ClientBuilder {
self.config.http2_keep_alive_while_idle = enabled;
self
}

// TCP options

/// Set whether sockets have `SO_NODELAY` enabled.
Expand Down

0 comments on commit 0b9b499

Please sign in to comment.