From 0e2f6e38e7b93e42099f546ef2c01629964d095a Mon Sep 17 00:00:00 2001 From: Yusuke Tanaka Date: Tue, 19 Nov 2024 10:48:57 +0900 Subject: [PATCH] feat(ext/fetch): Make fetch client parameters configurable (#26909) This commit makes HTTP client parameters used in `fetch` API configurable on the extension initialization via a callback `client_builder_hook` that users can provide. The main motivation behind this change is to allow `deno_fetch` users to tune the HTTP/2 client to suit their needs, although Deno CLI users will not benefit from it as no JavaScript interface is exposed to set these parameters currently. It is up to users whether to provide a hook function. If not provided, the default configuration from hyper crate will be used. Ref #26785 --- ext/fetch/lib.rs | 23 +++++++++++++++++++++-- ext/fetch/tests.rs | 1 + ext/kv/remote.rs | 1 + 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/ext/fetch/lib.rs b/ext/fetch/lib.rs index 5949f9f75f..c8e93b9fea 100644 --- a/ext/fetch/lib.rs +++ b/ext/fetch/lib.rs @@ -67,6 +67,7 @@ use http_body_util::BodyExt; use hyper::body::Frame; use hyper_util::client::legacy::connect::HttpConnector; use hyper_util::client::legacy::connect::HttpInfo; +use hyper_util::client::legacy::Builder as HyperClientBuilder; use hyper_util::rt::TokioExecutor; use hyper_util::rt::TokioTimer; use serde::Deserialize; @@ -85,6 +86,16 @@ pub struct Options { pub user_agent: String, pub root_cert_store_provider: Option>, pub proxy: Option, + /// A callback to customize HTTP client configuration. + /// + /// The settings applied with this hook may be overridden by the options + /// provided through `Deno.createHttpClient()` API. For instance, if the hook + /// calls [`hyper_util::client::legacy::Builder::pool_max_idle_per_host`] with + /// a value of 99, and a user calls `Deno.createHttpClient({ poolMaxIdlePerHost: 42 })`, + /// the value that will take effect is 42. + /// + /// For more info on what can be configured, see [`hyper_util::client::legacy::Builder`]. + pub client_builder_hook: Option HyperClientBuilder>, #[allow(clippy::type_complexity)] pub request_builder_hook: Option< fn(&mut http::Request) -> Result<(), deno_core::error::AnyError>, @@ -112,6 +123,7 @@ impl Default for Options { user_agent: "".to_string(), root_cert_store_provider: None, proxy: None, + client_builder_hook: None, request_builder_hook: None, unsafely_ignore_certificate_errors: None, client_cert_chain_and_key: TlsKeys::Null, @@ -271,6 +283,7 @@ pub fn create_client_from_options( pool_idle_timeout: None, http1: true, http2: true, + client_builder_hook: options.client_builder_hook, }, ) } @@ -908,6 +921,7 @@ where ), http1: args.http1, http2: args.http2, + client_builder_hook: options.client_builder_hook, }, )?; @@ -929,6 +943,7 @@ pub struct CreateHttpClientOptions { pub pool_idle_timeout: Option>, pub http1: bool, pub http2: bool, + pub client_builder_hook: Option HyperClientBuilder>, } impl Default for CreateHttpClientOptions { @@ -944,6 +959,7 @@ impl Default for CreateHttpClientOptions { pool_idle_timeout: None, http1: true, http2: true, + client_builder_hook: None, } } } @@ -999,11 +1015,14 @@ pub fn create_http_client( HttpClientCreateError::InvalidUserAgent(user_agent.to_string()) })?; - let mut builder = - hyper_util::client::legacy::Builder::new(TokioExecutor::new()); + let mut builder = HyperClientBuilder::new(TokioExecutor::new()); builder.timer(TokioTimer::new()); builder.pool_timer(TokioTimer::new()); + if let Some(client_builder_hook) = options.client_builder_hook { + builder = client_builder_hook(builder); + } + let mut proxies = proxy::from_env(); if let Some(proxy) = options.proxy { let mut intercept = proxy::Intercept::all(&proxy.url) diff --git a/ext/fetch/tests.rs b/ext/fetch/tests.rs index 5cd1a35a5e..e053c6b1cf 100644 --- a/ext/fetch/tests.rs +++ b/ext/fetch/tests.rs @@ -126,6 +126,7 @@ async fn rust_test_client_with_resolver( dns_resolver: resolver, http1: true, http2: true, + client_builder_hook: None, }, ) .unwrap(); diff --git a/ext/kv/remote.rs b/ext/kv/remote.rs index 63146daf71..1830aa67ee 100644 --- a/ext/kv/remote.rs +++ b/ext/kv/remote.rs @@ -210,6 +210,7 @@ impl DatabaseHandler pool_idle_timeout: None, http1: false, http2: true, + client_builder_hook: None, }, )?; let fetch_client = FetchClient(client);