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(ext/fetch): Make fetch client parameters configurable #26909

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
23 changes: 21 additions & 2 deletions ext/fetch/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -85,6 +86,16 @@ pub struct Options {
pub user_agent: String,
pub root_cert_store_provider: Option<Arc<dyn RootCertStoreProvider>>,
pub proxy: Option<Proxy>,
/// 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<fn(HyperClientBuilder) -> HyperClientBuilder>,
#[allow(clippy::type_complexity)]
pub request_builder_hook: Option<
fn(&mut http::Request<ReqBody>) -> Result<(), deno_core::error::AnyError>,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
},
)
}
Expand Down Expand Up @@ -908,6 +921,7 @@ where
),
http1: args.http1,
http2: args.http2,
client_builder_hook: options.client_builder_hook,
},
)?;

Expand All @@ -929,6 +943,7 @@ pub struct CreateHttpClientOptions {
pub pool_idle_timeout: Option<Option<u64>>,
pub http1: bool,
pub http2: bool,
pub client_builder_hook: Option<fn(HyperClientBuilder) -> HyperClientBuilder>,
}

impl Default for CreateHttpClientOptions {
Expand All @@ -944,6 +959,7 @@ impl Default for CreateHttpClientOptions {
pool_idle_timeout: None,
http1: true,
http2: true,
client_builder_hook: None,
}
}
}
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions ext/fetch/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ async fn rust_test_client_with_resolver(
dns_resolver: resolver,
http1: true,
http2: true,
client_builder_hook: None,
},
)
.unwrap();
Expand Down
1 change: 1 addition & 0 deletions ext/kv/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ impl<P: RemoteDbHandlerPermissions + 'static> DatabaseHandler
pool_idle_timeout: None,
http1: false,
http2: true,
client_builder_hook: None,
},
)?;
let fetch_client = FetchClient(client);
Expand Down