From b5706f2d8949cb4a0ecad49326b82a5ce197b1f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Fri, 22 May 2020 16:37:02 +0200 Subject: [PATCH] Make pool idle timeout configurable (#866) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit hyper’s ClientBuilder has an option to define the idle timeout of the connection pool. As it’s quite useful to be able to modify this value, this patch extends reqwest’s ClientBuilder to expose that option as well. The default value of 90 seconds is taken from hyper. --- src/async_impl/client.rs | 16 ++++++++++++++++ src/blocking/client.rs | 12 ++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/async_impl/client.rs b/src/async_impl/client.rs index 2525a02..362e1f8 100644 --- a/src/async_impl/client.rs +++ b/src/async_impl/client.rs @@ -76,6 +76,7 @@ struct Config { certs_verification: bool, connect_timeout: Option, connection_verbose: bool, + pool_idle_timeout: Option, max_idle_per_host: usize, #[cfg(feature = "__tls")] identity: Option, @@ -125,6 +126,7 @@ impl ClientBuilder { certs_verification: true, connect_timeout: None, connection_verbose: false, + pool_idle_timeout: Some(Duration::from_secs(90)), max_idle_per_host: std::usize::MAX, proxies: Vec::new(), auto_sys_proxy: true, @@ -304,6 +306,7 @@ impl ClientBuilder { builder.http2_initial_connection_window_size(http2_initial_connection_window_size); } + builder.pool_idle_timeout(config.pool_idle_timeout); builder.pool_max_idle_per_host(config.max_idle_per_host); if config.http1_title_case_headers { @@ -596,6 +599,19 @@ impl ClientBuilder { // HTTP options + /// Set an optional timeout for idle sockets being kept-alive. + /// + /// Pass `None` to disable timeout. + /// + /// Default is 90 seconds. + pub fn pool_idle_timeout(mut self, val: D) -> ClientBuilder + where + D: Into>, + { + self.config.pool_idle_timeout = val.into(); + self + } + /// Sets the maximum idle connection per host allowed in the pool. pub fn max_idle_per_host(mut self, max: usize) -> ClientBuilder { self.config.max_idle_per_host = max; diff --git a/src/blocking/client.rs b/src/blocking/client.rs index dcd1871..656d710 100644 --- a/src/blocking/client.rs +++ b/src/blocking/client.rs @@ -296,6 +296,18 @@ impl ClientBuilder { // HTTP options + /// Set an optional timeout for idle sockets being kept-alive. + /// + /// Pass `None` to disable timeout. + /// + /// Default is 90 seconds. + pub fn pool_idle_timeout(self, val: D) -> ClientBuilder + where + D: Into>, + { + self.with_inner(|inner| inner.pool_idle_timeout(val)) + } + /// Sets the maximum idle connection per host allowed in the pool. pub fn max_idle_per_host(self, max: usize) -> ClientBuilder { self.with_inner(move |inner| inner.max_idle_per_host(max))