Unverified Commit 9d44731a authored by Benjamin Woodruff's avatar Benjamin Woodruff Committed by GitHub

Turbopack: Add cache for reqwest clients (#81742)

We need a cache because:
- This will allow us to enable HTTP keep-alive in a subsequent PR which would allow connection-reuse.
- Using `rustls-tls-native` may make client construction far more expensive: #79060

Clients don't fit turbo-task's caching architecture and are not serializable, so the cache lives outside of turbo-tasks. I'm re-using the https://docs.rs/quick_cache/latest/quick_cache/ crate for this, which we already pull in for turbo-persistence.
parent 5e46e372
......@@ -2799,7 +2799,7 @@ dependencies = [
"httpdate",
"itoa",
"pin-project-lite",
"socket2 0.4.9",
"socket2 0.5.10",
"tokio",
"tower-service",
"tracing",
......@@ -5460,9 +5460,9 @@ checksum = "a993555f31e5a609f617c12db6250dedcac1b0a85076912c436e6fc9b2c8e6a3"
[[package]]
name = "quick_cache"
version = "0.6.11"
version = "0.6.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0af25b4e960ffdf0dead61cf0cec0c2e44c76927bf933ab4f02e2858fb449397"
checksum = "6b450dad8382b1b95061d5ca1eb792081fb082adf48c678791fe917509596d5f"
dependencies = [
"ahash 0.8.12",
"equivalent",
......@@ -9338,7 +9338,9 @@ version = "0.1.0"
dependencies = [
"anyhow",
"mockito",
"quick_cache",
"reqwest 0.12.20",
"serde",
"tokio",
"turbo-rcstr",
"turbo-tasks",
......
......@@ -390,6 +390,7 @@ pin-project-lite = "0.2.9"
postcard = "1.0.4"
proc-macro2 = "1.0.79"
qstring = "0.7.2"
quick_cache = { version = "0.6.14" }
quote = "1.0.23"
rand = "0.9.0"
rayon = "1.10.0"
......
......@@ -9,7 +9,7 @@ use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{Completion, FxIndexMap, ResolvedVc, Vc};
use turbo_tasks_bytes::stream::SingleValue;
use turbo_tasks_env::{CommandLineProcessEnv, ProcessEnv};
use turbo_tasks_fetch::{HttpResponseBody, fetch};
use turbo_tasks_fetch::{HttpResponseBody, ReqwestClientConfig, fetch};
use turbo_tasks_fs::{
DiskFileSystem, File, FileContent, FileSystem, FileSystemPath,
json::parse_json_with_source_context,
......@@ -663,7 +663,7 @@ async fn fetch_from_google_fonts(
let result = fetch(
url,
Some(rcstr!(USER_AGENT_FOR_GOOGLE_FONTS)),
Vc::cell(None),
ReqwestClientConfig { proxy: None }.cell(),
)
.await?;
......
......@@ -21,7 +21,7 @@ lzzzz = "1.1.0"
memmap2 = "0.9.5"
parking_lot = { workspace = true }
qfilter = { version = "0.2.4", features = ["serde"] }
quick_cache = { version = "0.6.9" }
quick_cache = { workspace = true }
rayon = { workspace = true }
rustc-hash = { workspace = true }
smallvec = { workspace = true}
......
......@@ -25,7 +25,9 @@ reqwest = { workspace = true, features = ["rustls-tls"] }
[dependencies]
anyhow = { workspace = true }
quick_cache = { workspace = true }
reqwest = { workspace = true }
serde = { workspace = true }
tokio = { workspace = true }
turbo-rcstr = { workspace = true }
turbo-tasks = { workspace = true }
......
......@@ -2,12 +2,20 @@
#![feature(arbitrary_self_types)]
#![feature(arbitrary_self_types_pointers)]
mod reqwest_client_cache;
use anyhow::Result;
use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ResolvedVc, Vc, duration_span, mark_session_dependent};
use turbo_tasks_fs::FileSystemPath;
use turbopack_core::issue::{Issue, IssueSeverity, IssueStage, OptionStyledString, StyledString};
use crate::reqwest_client_cache::try_get_cached_reqwest_client;
pub use crate::reqwest_client_cache::{
__test_only_reqwest_client_cache_clear, __test_only_reqwest_client_cache_len, ProxyConfig,
ReqwestClientConfig,
};
pub fn register() {
turbo_tasks::register();
turbo_tasks_fs::register();
......@@ -38,60 +46,47 @@ impl HttpResponseBody {
}
}
#[turbo_tasks::value(shared)]
#[derive(Debug)]
pub enum ProxyConfig {
Http(String),
Https(String),
}
#[turbo_tasks::value(transparent)]
pub struct OptionProxyConfig(Option<ProxyConfig>);
#[turbo_tasks::function(network)]
pub async fn fetch(
url: RcStr,
user_agent: Option<RcStr>,
proxy_option: Vc<OptionProxyConfig>,
client_config: Vc<ReqwestClientConfig>,
) -> Result<Vc<FetchResult>> {
let proxy_option = &*proxy_option.await?;
let url_ref = &*url;
let client_config = client_config.await?;
let response_result: reqwest::Result<HttpResponse> = async move {
let client = try_get_cached_reqwest_client(client_config)?;
let mut builder = client.get(url_ref);
if let Some(user_agent) = user_agent {
builder = builder.header("User-Agent", user_agent.as_str());
}
let client_builder = reqwest::Client::builder();
let client_builder = match proxy_option {
Some(ProxyConfig::Http(proxy)) => client_builder.proxy(reqwest::Proxy::http(proxy)?),
Some(ProxyConfig::Https(proxy)) => client_builder.proxy(reqwest::Proxy::https(proxy)?),
_ => client_builder,
};
let response = {
let _span = duration_span!("fetch request", url = url_ref);
builder.send().await
}
.and_then(|r| r.error_for_status())?;
let client = client_builder.build()?;
let status = response.status().as_u16();
let mut builder = client.get(url.as_str());
if let Some(user_agent) = user_agent {
builder = builder.header("User-Agent", user_agent.as_str());
}
let body = {
let _span = duration_span!("fetch response", url = url_ref);
response.bytes().await?
}
.to_vec();
let response = {
let _span = duration_span!("fetch request", url = url.as_str());
builder.send().await
Ok(HttpResponse {
status,
body: HttpResponseBody(body).resolved_cell(),
})
}
.and_then(|r| r.error_for_status());
match response {
Ok(response) => {
let status = response.status().as_u16();
let body = {
let _span = duration_span!("fetch response", url = url.as_str());
response.bytes().await?
}
.to_vec();
Ok(Vc::cell(Ok(HttpResponse {
status,
body: HttpResponseBody(body).resolved_cell(),
}
.resolved_cell())))
}
.await;
match response_result {
Ok(resp) => Ok(Vc::cell(Ok(resp.resolved_cell()))),
Err(err) => {
// the client failed to construct or the HTTP request failed
mark_session_dependent();
Ok(Vc::cell(Err(
FetchError::from_reqwest_error(&err, &url).resolved_cell()
......
use std::{hash::Hash, sync::LazyLock};
use quick_cache::sync::Cache;
use serde::{Deserialize, Serialize};
use turbo_rcstr::RcStr;
use turbo_tasks::{NonLocalValue, ReadRef, trace::TraceRawVcs};
const MAX_CLIENTS: usize = 16;
static CLIENT_CACHE: LazyLock<Cache<ReadRef<ReqwestClientConfig>, reqwest::Client>> =
LazyLock::new(|| Cache::new(MAX_CLIENTS));
#[derive(Hash, PartialEq, Eq, Serialize, Deserialize, NonLocalValue, Debug, TraceRawVcs)]
pub enum ProxyConfig {
Http(RcStr),
Https(RcStr),
}
/// Represents the configuration needed to construct a [`reqwest::Client`].
///
/// This is used to cache clients keyed by their configuration, so the configuration should contain
/// as few fields as possible and change infrequently.
///
/// This is needed because [`reqwest::ClientBuilder`] does not implement the required traits. This
/// factory cannot be a closure because closures do not implement `Eq` or `Hash`.
#[turbo_tasks::value(shared)]
#[derive(Hash)]
pub struct ReqwestClientConfig {
pub proxy: Option<ProxyConfig>,
}
impl ReqwestClientConfig {
fn try_build(&self) -> reqwest::Result<reqwest::Client> {
let mut client_builder = reqwest::Client::builder();
match &self.proxy {
Some(ProxyConfig::Http(proxy)) => {
client_builder = client_builder.proxy(reqwest::Proxy::http(proxy.as_str())?)
}
Some(ProxyConfig::Https(proxy)) => {
client_builder = client_builder.proxy(reqwest::Proxy::https(proxy.as_str())?)
}
None => {}
};
client_builder.build()
}
}
/// Given a config, returns a cached instance if it exists, otherwise constructs a new one.
///
/// The cache is bound in size to prevent accidental blowups or leaks. However, in practice, very
/// few clients should be created, likely only when the bundler configuration changes.
///
/// Client construction is largely deterministic, aside from changes to system TLS configuration.
///
/// The reqwest client fails to construct if the TLS backend cannot be initialized, or the resolver
/// cannot load the system configuration. These failures should be treated as cached for some amount
/// of time, but ultimately transient (e.g. using [`turbo_tasks::mark_session_dependent`]).
pub fn try_get_cached_reqwest_client(
config: ReadRef<ReqwestClientConfig>,
) -> reqwest::Result<reqwest::Client> {
CLIENT_CACHE.get_or_insert_with(&config, {
let config = ReadRef::clone(&config);
move || config.try_build()
})
}
#[doc(hidden)]
pub fn __test_only_reqwest_client_cache_clear() {
CLIENT_CACHE.clear()
}
#[doc(hidden)]
pub fn __test_only_reqwest_client_cache_len() -> usize {
CLIENT_CACHE.len()
}
#![allow(clippy::needless_return)] // tokio macro-generated code doesn't respect this
#![cfg(test)]
use tokio::sync::Mutex as TokioMutex;
use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::Vc;
use turbo_tasks_fetch::{FetchErrorKind, fetch};
use turbo_tasks_fetch::{
__test_only_reqwest_client_cache_clear, __test_only_reqwest_client_cache_len, FetchErrorKind,
ProxyConfig, ReqwestClientConfig, fetch,
};
use turbo_tasks_fs::{DiskFileSystem, FileSystem, FileSystemPath};
use turbo_tasks_testing::{Registration, register, run};
use turbopack_core::issue::{Issue, IssueSeverity, StyledString};
static REGISTRATION: Registration = register!(turbo_tasks_fetch::register);
/// We inspect information about the global client cache, so *every* test in this process *must*
/// acquire and hold this lock to prevent potential flakiness.
static GLOBAL_TEST_LOCK: TokioMutex<()> = TokioMutex::const_new(());
#[tokio::test]
async fn basic_get() {
let _guard = GLOBAL_TEST_LOCK.lock().await;
run(&REGISTRATION, || async {
let mut server = mockito::Server::new_async().await;
let resource_mock = server
......@@ -20,10 +29,11 @@ async fn basic_get() {
.create_async()
.await;
let config_vc = ReqwestClientConfig { proxy: None }.cell();
let response = &*fetch(
RcStr::from(format!("{}/foo.woff", server.url())),
/* user_agent */ None,
/* proxy */ Vc::cell(None),
config_vc,
)
.await?
.unwrap()
......@@ -41,6 +51,7 @@ async fn basic_get() {
#[tokio::test]
async fn sends_user_agent() {
let _guard = GLOBAL_TEST_LOCK.lock().await;
run(&REGISTRATION, || async {
let mut server = mockito::Server::new_async().await;
let resource_mock = server
......@@ -52,10 +63,11 @@ async fn sends_user_agent() {
eprintln!("{}", server.url());
let config_vc = ReqwestClientConfig { proxy: None }.cell();
let response = &*fetch(
RcStr::from(format!("{}/foo.woff", server.url())),
Some(rcstr!("mock-user-agent")),
/* proxy */ Vc::cell(None),
config_vc,
)
.await?
.unwrap()
......@@ -75,6 +87,7 @@ async fn sends_user_agent() {
// TODO: Implement invalidation that respects Cache-Control headers.
#[tokio::test]
async fn invalidation_does_not_invalidate() {
let _guard = GLOBAL_TEST_LOCK.lock().await;
run(&REGISTRATION, || async {
let mut server = mockito::Server::new_async().await;
let resource_mock = server
......@@ -85,8 +98,8 @@ async fn invalidation_does_not_invalidate() {
.await;
let url = RcStr::from(format!("{}/foo.woff", server.url()));
let proxy_vc = Vc::cell(None);
let response = &*fetch(url.clone(), /* user_agent */ None, proxy_vc)
let config_vc = ReqwestClientConfig { proxy: None }.cell();
let response = &*fetch(url.clone(), /* user_agent */ None, config_vc)
.await?
.unwrap()
.await?;
......@@ -96,7 +109,7 @@ async fn invalidation_does_not_invalidate() {
assert_eq!(response.status, 200);
assert_eq!(*response.body.to_string().await?, "responsebody");
let second_response = &*fetch(url.clone(), /* user_agent */ None, proxy_vc)
let second_response = &*fetch(url.clone(), /* user_agent */ None, config_vc)
.await?
.unwrap()
.await?;
......@@ -111,14 +124,20 @@ async fn invalidation_does_not_invalidate() {
.unwrap()
}
fn get_issue_context() -> Vc<FileSystemPath> {
DiskFileSystem::new(rcstr!("root"), rcstr!("/"), vec![]).root()
}
#[tokio::test]
async fn errors_on_failed_connection() {
let _guard = GLOBAL_TEST_LOCK.lock().await;
run(&REGISTRATION, || async {
// Try to connect to port 0 on localhost, which is never valid and immediately returns
// `ECONNREFUSED`.
// Other values (e.g. domain name, reserved IP address block) may result in long timeouts.
let url = rcstr!("http://127.0.0.1:0/foo.woff");
let response_vc = fetch(url.clone(), None, Vc::cell(None));
let config_vc = ReqwestClientConfig { proxy: None }.cell();
let response_vc = fetch(url.clone(), None, config_vc);
let err_vc = &*response_vc.await?.unwrap_err();
let err = err_vc.await?;
......@@ -142,6 +161,7 @@ async fn errors_on_failed_connection() {
#[tokio::test]
async fn errors_on_404() {
let _guard = GLOBAL_TEST_LOCK.lock().await;
run(&REGISTRATION, || async {
let mut server = mockito::Server::new_async().await;
let resource_mock = server
......@@ -151,7 +171,8 @@ async fn errors_on_404() {
.await;
let url = RcStr::from(server.url());
let response_vc = fetch(url.clone(), None, Vc::cell(None));
let config_vc = ReqwestClientConfig { proxy: None }.cell();
let response_vc = fetch(url.clone(), None, config_vc);
let err_vc = &*response_vc.await?.unwrap_err();
let err = err_vc.await?;
......@@ -173,6 +194,61 @@ async fn errors_on_404() {
.unwrap()
}
fn get_issue_context() -> Vc<FileSystemPath> {
DiskFileSystem::new(rcstr!("root"), rcstr!("/"), vec![]).root()
#[tokio::test]
async fn client_cache() {
// a simple fetch that should always succeed
async fn simple_fetch(path: &str, config: ReqwestClientConfig) -> anyhow::Result<()> {
let mut server = mockito::Server::new_async().await;
let _resource_mock = server
.mock("GET", &*format!("/{path}"))
.with_body("responsebody")
.create_async()
.await;
let url = RcStr::from(format!("{}/{}", server.url(), path));
let response = match &*fetch(url.clone(), /* user_agent */ None, config.cell()).await? {
Ok(resp) => resp.await?,
Err(_err) => {
anyhow::bail!("fetch error")
}
};
if response.status != 200 {
anyhow::bail!("non-200 status code")
}
anyhow::Ok(())
}
let _guard = GLOBAL_TEST_LOCK.lock().await;
run(&REGISTRATION, || async {
__test_only_reqwest_client_cache_clear();
assert_eq!(__test_only_reqwest_client_cache_len(), 0);
simple_fetch("/foo", ReqwestClientConfig { proxy: None })
.await
.unwrap();
assert_eq!(__test_only_reqwest_client_cache_len(), 1);
// the client is reused if the config is the same (by equality)
simple_fetch("/bar", ReqwestClientConfig { proxy: None })
.await
.unwrap();
assert_eq!(__test_only_reqwest_client_cache_len(), 1);
// the client is recreated if the config is different
simple_fetch(
"/bar",
ReqwestClientConfig {
proxy: Some(ProxyConfig::Http(RcStr::from("http://127.0.0.1:0/"))),
},
)
.await
.unwrap_err();
assert_eq!(__test_only_reqwest_client_cache_len(), 2);
Ok(())
})
.await
.unwrap()
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment