This commit is contained in:
Mateusz Gruszczyński committed 2026-11-20 23:49:55 +01:00
1 parent 5b2eaaff61
commit 3b5fc8dce1
5 files changed
+175 -2

No files matched your search

@@ -109,6 +109,19 @@ mod static_cookie_policy_tests {
self.request(Method::GET, path, headers).await
}
async fn change(&self, edit: impl FnOnce(&mut Config)) {
let current = self.app.config.load_full();
let mut config = current.envelope.config.clone();
edit(&mut config);
self.app
.update(crate::config::Update {
expected_revision: current.envelope.revision,
config,
})
.await
.unwrap();
}
async fn stop(self) {
self.app.shutdown.cancel();
self.app.tunnels.close();
@@ -667,4 +680,62 @@ mod static_cookie_policy_tests {
);
fixture.stop().await;
}
#[tokio::test]
async fn cache_tuning_saves_keep_objects_but_policy_saves_purge() {
let origin = start_origin().await;
let fixture = Fixture::new(test_config(origin.addr, shipped_rules())).await;
assert_eq!(fixture.get("/assets/app.css", &[]).await.cache(), "MISS");
assert_eq!(
fixture
.get("/assets/auth.css", &[("cookie", SESSION)])
.await
.cache(),
"MISS"
);
fixture
.change(|config| {
config.cache.max_entries = 2048;
config.cache.max_bytes *= 2;
config.cache.collapse_enabled = true;
config.cache.stale_if_error_statuses = vec![503];
config.cache.proactive_refresh_enabled = true;
config.cache.hot_auto_trim_after_seconds = 600;
})
.await;
assert_eq!(fixture.get("/assets/app.css", &[]).await.cache(), "HIT");
assert_eq!(
fixture
.get("/assets/auth.css", &[("cookie", SESSION)])
.await
.cache(),
"HIT"
);
// A lowered entry limit evicts instead of purging.
fixture.change(|config| config.cache.max_entries = 1).await;
let kept = [
fixture.get("/assets/app.css", &[]).await.cache().to_owned(),
fixture
.get("/assets/auth.css", &[])
.await
.cache()
.to_owned(),
];
assert!(kept.iter().filter(|c| *c == "HIT").count() <= 1, "{kept:?}");
fixture
.change(|config| config.cache.max_entries = 4096)
.await;
assert_eq!(fixture.get("/fonts/site.woff2", &[]).await.cache(), "MISS");
assert_eq!(fixture.get("/fonts/site.woff2", &[]).await.cache(), "HIT");
// Representation/privacy changes still purge.
fixture
.change(|config| config.cache.default_ttl_seconds = 30)
.await;
assert_eq!(fixture.get("/fonts/site.woff2", &[]).await.cache(), "MISS");
fixture
.change(|config| config.cache.static_ignore_vary_cookie = false)
.await;
assert_eq!(fixture.get("/fonts/site.woff2", &[]).await.cache(), "MISS");
fixture.stop().await;
}
}
+54 -1
View File
@@ -132,8 +132,61 @@ mod cache_revision_tests {
CacheInvalidation::None
));
// Operational cache tuning keeps HOT/COLD objects.
let mut tuning = base.clone();
tuning.cache.max_entries += 1;
tuning.cache.max_bytes *= 2;
tuning.cache.max_object_bytes /= 2;
tuning.cache.transient_max_bytes *= 2;
tuning.cache.hot_eviction_max_objects += 1;
tuning.cache.hot_auto_trim_enabled = !base.cache.hot_auto_trim_enabled;
tuning.cache.hot_auto_trim_after_seconds += 10;
tuning.cache.hot_auto_trim_target_percent = 70;
tuning.cache.cold_max_bytes *= 2;
tuning.cache.cold_max_entries += 1;
tuning.cache.cold_gc_interval_seconds += 1;
tuning.cache.cold_gc_max_delete += 1;
tuning.cache.cold_after_seconds += 1;
tuning.cache.cold_on_idle = !base.cache.cold_on_idle;
tuning.cache.cold_max_hot_seconds += 1;
tuning.cache.cold_on_age = !base.cache.cold_on_age;
tuning.cache.cold_on_lru = !base.cache.cold_on_lru;
tuning.cache.cold_on_shutdown = !base.cache.cold_on_shutdown;
tuning.cache.collapse_enabled = !base.cache.collapse_enabled;
tuning.cache.collapse_wait_timeout_ms += 1;
tuning.cache.stale_if_error_timeout = !base.cache.stale_if_error_timeout;
tuning.cache.stale_if_error_statuses = vec![503];
tuning.cache.proactive_refresh_enabled = !base.cache.proactive_refresh_enabled;
tuning.cache.proactive_refresh_percent = 20;
tuning.cache.proactive_refresh_max_inflight = 8;
assert_eq!(cache_revision(&tuning), identity);
assert!(matches!(
cache_invalidation(&base, &tuning),
CacheInvalidation::None
));
// Representation and privacy settings still purge everything.
for change in [
(|c: &mut config::Config| c.cache.default_ttl_seconds += 60) as fn(&mut config::Config),
|c| c.cache.max_ttl_seconds += 60,
|c| c.cache.varnish_grace_seconds += 1,
|c| c.cache.varnish_compatible = !c.cache.varnish_compatible,
|c| c.cache.static_ignore_cookies = !c.cache.static_ignore_cookies,
|c| c.cache.static_ignore_vary_cookie = !c.cache.static_ignore_vary_cookie,
|c| c.cache.static_extensions.push("json".into()),
|c| c.cache.cold_enabled = !c.cache.cold_enabled,
|c| c.cache.rules.push(serde_json::from_value(serde_json::json!(
{"id": "x", "conditions": [{"field": "path", "operator": "prefix", "values": ["/a"]}],
"actions": [{"type": "pass"}]})).unwrap()),
] {
let mut changed = base.clone();
change(&mut changed);
assert_ne!(cache_revision(&changed), identity);
assert!(matches!(cache_invalidation(&base, &changed), CacheInvalidation::All(_)));
}
let mut cache = base.clone();
cache.cache.max_entries += 1;
cache.cache.default_ttl_seconds += 1;
let cache_identity = cache_revision(&cache);
assert_ne!(cache_identity, identity);
assert!(matches!(
+39
View File
@@ -100,6 +100,45 @@ fn cache_revision(config: &config::Config) -> u64 {
cache.expose_only_hits = false;
cache.expose_detailed_status = false;
cache.via_header = None;
// Operational tuning: memory/eviction limits, COLD demotion and GC,
// request coalescing, stale-if-error triggers and proactive refresh.
// They change how the cache is run, not what a stored object contains or
// who may receive it, so changing them must not purge HOT/COLD. New limits
// apply immediately (`Cache::configure` evicts above a lowered limit).
// Representation/privacy fields (enabled, varnish_compatible, TTL/grace,
// static Cookie policy, rules, cold_enabled/cold_path) stay in the digest.
let neutral = crate::cache::Settings::default();
cache.max_bytes = neutral.max_bytes;
cache.max_object_bytes = neutral.max_object_bytes;
cache.max_entries = neutral.max_entries;
cache.transient_max_bytes = neutral.transient_max_bytes;
cache.cold_queue_max_bytes = neutral.cold_queue_max_bytes;
cache.hot_eviction_max_objects = neutral.hot_eviction_max_objects;
cache.hot_auto_trim_enabled = neutral.hot_auto_trim_enabled;
cache.hot_auto_trim_after_seconds = neutral.hot_auto_trim_after_seconds;
cache.hot_auto_trim_target_percent = neutral.hot_auto_trim_target_percent;
cache.cold_max_bytes = neutral.cold_max_bytes;
cache.cold_max_entries = neutral.cold_max_entries;
cache.cold_gc_interval_seconds = neutral.cold_gc_interval_seconds;
cache.cold_gc_max_delete = neutral.cold_gc_max_delete;
cache.cold_after_seconds = neutral.cold_after_seconds;
cache.cold_on_idle = neutral.cold_on_idle;
cache.cold_max_hot_seconds = neutral.cold_max_hot_seconds;
cache.cold_on_age = neutral.cold_on_age;
cache.cold_on_lru = neutral.cold_on_lru;
cache.cold_on_shutdown = neutral.cold_on_shutdown;
cache.collapse_enabled = neutral.collapse_enabled;
cache.collapse_wait_timeout_ms = neutral.collapse_wait_timeout_ms;
cache.collapse_timeout_fallback = neutral.collapse_timeout_fallback;
cache.stale_if_error_timeout = neutral.stale_if_error_timeout;
cache.stale_if_error_connection = neutral.stale_if_error_connection;
cache.stale_if_error_dns = neutral.stale_if_error_dns;
cache.stale_if_error_tls = neutral.stale_if_error_tls;
cache.stale_if_error_statuses = neutral.stale_if_error_statuses;
cache.proactive_refresh_enabled = neutral.proactive_refresh_enabled;
cache.proactive_refresh_before_expiry_seconds = neutral.proactive_refresh_before_expiry_seconds;
cache.proactive_refresh_percent = neutral.proactive_refresh_percent;
cache.proactive_refresh_max_inflight = neutral.proactive_refresh_max_inflight;
let mut transform = config.transform.clone();
transform.preset = None;
transform.memory_budget_bytes = 0;