This commit is contained in:
Mateusz Gruszczyński committed 2026-10-25 10:15:26 +01:00
1 parent b3dfd570b5
commit a4042d6439
16 files changed
+362 -55

No files matched your search

+79 -9
View File
@@ -171,7 +171,7 @@ impl Settings {
.any(|item| item.eq_ignore_ascii_case(extension))
}
pub fn annotate(&self, headers: &mut HeaderMap, outcome: &'static str, hits: u64, ttl: u64) {
self.annotate_internal(headers, outcome, hits, ttl, None, false);
self.annotate_internal(headers, outcome, hits, ttl as i64, None, false, None);
}
pub fn annotate_reason(
@@ -182,7 +182,15 @@ impl Settings {
ttl: u64,
reason: &'static str,
) {
self.annotate_internal(headers, outcome, hits, ttl, Some(reason), false);
self.annotate_internal(
headers,
outcome,
hits,
ttl as i64,
Some(reason),
false,
None,
);
}
pub fn annotate_reason_transformed(
@@ -194,7 +202,57 @@ impl Settings {
reason: &'static str,
transformed: bool,
) {
self.annotate_internal(headers, outcome, hits, ttl, Some(reason), transformed);
self.annotate_internal(
headers,
outcome,
hits,
ttl as i64,
Some(reason),
transformed,
None,
);
}
pub fn annotate_reason_transformed_freshness(
&self,
headers: &mut HeaderMap,
outcome: &'static str,
hits: u64,
freshness_ttl: i64,
reason: &'static str,
transformed: bool,
) {
self.annotate_internal(
headers,
outcome,
hits,
freshness_ttl,
Some(reason),
transformed,
None,
);
}
pub fn annotate_reason_transformed_forwarded(
&self,
headers: &mut HeaderMap,
outcome: &'static str,
hits: u64,
ttl: u64,
reason: &'static str,
transformed: bool,
forward: &'static str,
forward_status: Option<StatusCode>,
) {
self.annotate_internal(
headers,
outcome,
hits,
ttl as i64,
Some(reason),
transformed,
Some((forward, forward_status)),
);
}
fn annotate_internal(
@@ -202,9 +260,10 @@ impl Settings {
headers: &mut HeaderMap,
outcome: &'static str,
hits: u64,
ttl: u64,
ttl: i64,
reason: Option<&'static str>,
transformed: bool,
forwarded: Option<(&'static str, Option<StatusCode>)>,
) {
if !self.enabled {
return;
@@ -245,15 +304,26 @@ impl Settings {
);
}
if self.expose_cache_status {
let mut value = if matches!(
reason,
Some("cache_revalidated" | "cache_revalidated_uncached")
) {
"RustProxy; fwd=stale; fwd-status=304".to_owned()
let forwarded = forwarded.or_else(|| {
matches!(
reason,
Some("cache_revalidated" | "cache_revalidated_uncached")
)
.then_some(("stale", Some(StatusCode::NOT_MODIFIED)))
});
let mut value = if let Some((why, status)) = forwarded {
let mut value = format!("RustProxy; fwd={why}");
if let Some(status) = status {
value.push_str(&format!("; fwd-status={}", status.as_u16()));
}
value
} else {
match outcome {
"HIT" => format!("RustProxy; hit; ttl={ttl}"),
"MISS" => "RustProxy; fwd=miss".to_owned(),
"BYPASS" if reason == Some("request_method") => {
"RustProxy; fwd=method".to_owned()
}
_ => "RustProxy; fwd=bypass".to_owned(),
}
};
+15
View File
@@ -126,12 +126,18 @@ impl Drop for ColdReadGuard<'_> {
}
}
}
fn cache_status_ttl(ttl: u64, age: u64) -> i64 {
const MAX: i128 = 999_999_999_999_999;
(i128::from(ttl) - i128::from(age)).clamp(-MAX, MAX) as i64
}
pub struct Hit {
pub response: Response,
body: Bytes,
pub hits: u64,
pub transformed: bool,
pub remaining_ttl: u64,
pub cache_status_ttl: i64,
pub resident_age_seconds: u64,
pub stale: bool,
pub background_revalidate: bool,
@@ -163,6 +169,15 @@ impl Revalidation {
pub fn reusable_without_validation(&self) -> bool {
self.reusable_without_validation
}
pub fn cache_status_forward_reason(&self, request_forces_validation: bool) -> &'static str {
if self.age >= self.ttl {
"stale"
} else if request_forces_validation && self.reusable_without_validation {
"request"
} else {
"miss"
}
}
pub fn headers(&self) -> &HeaderMap {
&self.headers
}