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

feat: add connection_cache_size to Config #96

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
14 changes: 13 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ pub struct Config {
/// - `hyper_client`: No effect. Hyper does not support such an option.
/// - `wasm_client`: No effect. Web browsers do not support such an option.
pub max_connections_per_host: usize,
/// Maximum number of simultaneously open persistent connections that this client may cache in the pool.
///
/// Default: `5`.
pub connection_cache_size: usize,
/// TLS Configuration (Rustls)
#[cfg_attr(feature = "docs", doc(cfg(feature = "h1_client")))]
#[cfg(all(feature = "h1_client", feature = "rustls"))]
Expand All @@ -51,7 +55,8 @@ impl Debug for Config {
.field("http_keep_alive", &self.http_keep_alive)
.field("tcp_no_delay", &self.tcp_no_delay)
.field("timeout", &self.timeout)
.field("max_connections_per_host", &self.max_connections_per_host);
.field("max_connections_per_host", &self.max_connections_per_host)
.field("connection_cache_size", &self.connection_cache_size);

#[cfg(all(feature = "h1_client", feature = "rustls"))]
{
Expand All @@ -78,6 +83,7 @@ impl Config {
tcp_no_delay: false,
timeout: Some(Duration::from_secs(60)),
max_connections_per_host: 50,
connection_cache_size: 5,
#[cfg(all(feature = "h1_client", any(feature = "rustls", feature = "native-tls")))]
tls_config: None,
}
Expand Down Expand Up @@ -115,6 +121,12 @@ impl Config {
self
}

/// Set the maximum number of simultaneously open persistent connections that this client may cache in the pool.
pub fn set_connection_cache_size(mut self, connection_cache_size: usize) -> Self {
self.connection_cache_size = connection_cache_size;
self
}

/// Set TLS Configuration (Rustls)
#[cfg_attr(feature = "docs", doc(cfg(feature = "h1_client")))]
#[cfg(all(feature = "h1_client", feature = "rustls"))]
Expand Down
7 changes: 6 additions & 1 deletion src/isahc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ impl HttpClient for IsahcClient {

if !config.http_keep_alive {
builder = builder.connection_cache_size(0);
} else {
builder = builder.connection_cache_size(config.connection_cache_size);
}
if config.tcp_no_delay {
builder = builder.tcp_nodelay();
Expand All @@ -106,10 +108,13 @@ impl TryFrom<Config> for IsahcClient {
type Error = isahc::Error;

fn try_from(config: Config) -> Result<Self, Self::Error> {
let mut builder = isahc::HttpClient::builder();
let mut builder =
isahc::HttpClient::builder().max_connections_per_host(config.max_connections_per_host);

if !config.http_keep_alive {
builder = builder.connection_cache_size(0);
} else {
builder = builder.connection_cache_size(config.connection_cache_size);
}
if config.tcp_no_delay {
builder = builder.tcp_nodelay();
Expand Down