Skip to content

Commit 8a4d243

Browse files
goffrieConvex, Inc.
authored andcommitted
Set the size of the global http_client cache in bytes (#40974)
GitOrigin-RevId: 5e2a7196955aa8532c183819bb8f3050c54ef19f
1 parent e3ac9f4 commit 8a4d243

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/common/src/knobs.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,3 +1415,8 @@ pub static SUBSCRIPTION_ADVANCE_LOG_TRACING_THRESHOLD: LazyLock<u64> =
14151415
/// How many concurrent index backfill threads to run concurrently.
14161416
pub static INDEX_BACKFILL_CONCURRENCY: LazyLock<usize> =
14171417
LazyLock::new(|| env_config("INDEX_BACKFILL_CONCURRENCY", 8));
1418+
1419+
/// The max size of the global HTTP cache, in bytes. This is only used for
1420+
/// getting auth metadata right now.
1421+
pub static HTTP_CACHE_SIZE: LazyLock<u64> =
1422+
LazyLock::new(|| env_config("HTTP_CACHE_SIZE", 16 * 1024 * 1024));

crates/http_client/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ doctest = false
1212
[dependencies]
1313
anyhow = { workspace = true }
1414
bytes = { workspace = true }
15+
common = { workspace = true }
1516
futures = { workspace = true }
1617
http-body-util = { workspace = true }
1718
http-cache = { workspace = true }
@@ -24,6 +25,7 @@ strum = { workspace = true }
2425
thiserror = { workspace = true }
2526

2627
[dev-dependencies]
28+
common = { workspace = true, features = ["testing"] }
2729
metrics = { workspace = true, features = ["testing"] }
2830
tokio = { workspace = true }
2931

crates/http_client/src/lib.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
#![feature(try_blocks)]
22
#![feature(impl_trait_in_fn_trait_return)]
33

4-
use std::sync::LazyLock;
4+
use std::sync::{
5+
Arc,
6+
LazyLock,
7+
};
58

69
use bytes::BufMut;
10+
use common::knobs::HTTP_CACHE_SIZE;
711
use futures::Future;
812
use http_body_util::BodyExt;
9-
use http_cache::XCACHE;
13+
use http_cache::{
14+
MokaCache,
15+
XCACHE,
16+
};
1017
use http_cache_reqwest::{
1118
Cache,
1219
CacheMode,
@@ -29,7 +36,16 @@ mod metrics;
2936
#[error(transparent)]
3037
pub struct AsStdError(#[from] anyhow::Error);
3138

32-
static CACHE: LazyLock<MokaManager> = LazyLock::new(MokaManager::default);
39+
static CACHE: LazyLock<MokaManager> = LazyLock::new(|| {
40+
MokaManager::new(
41+
MokaCache::builder()
42+
.max_capacity(*HTTP_CACHE_SIZE)
43+
.weigher(|k: &String, v: &Arc<Vec<u8>>| {
44+
u32::try_from(k.len() + v.len()).unwrap_or(u32::MAX)
45+
})
46+
.build(),
47+
)
48+
});
3349
static HTTP_CLIENT: LazyLock<reqwest_middleware::ClientWithMiddleware> = LazyLock::new(|| {
3450
ClientBuilder::new(Client::new())
3551
.with(Cache(HttpCache {

0 commit comments

Comments
 (0)