aboutsummaryrefslogtreecommitdiffhomepage
path: root/pingora-cache
diff options
context:
space:
mode:
authorEdward Wang <[email protected]>2024-08-02 16:41:20 -0700
committerAndrew Hauck <[email protected]>2024-08-09 14:30:49 -0700
commit7003ac34ad514edce4b9cda235ebdb498acd93f1 (patch)
treee194fff39a2b6fab7f584cd3daca3c249a2cae1e /pingora-cache
parent35810d6c923eca12c516bc119e982737e30f824c (diff)
downloadpingora-7003ac34ad514edce4b9cda235ebdb498acd93f1.tar.gz
pingora-7003ac34ad514edce4b9cda235ebdb498acd93f1.zip
Set stale-updating as an explicit CachePhase
This cache phase indicates when we are serving a stale cache hit, but there is another request currently updating the asset.
Diffstat (limited to 'pingora-cache')
-rw-r--r--pingora-cache/src/lib.rs19
1 files changed, 17 insertions, 2 deletions
diff --git a/pingora-cache/src/lib.rs b/pingora-cache/src/lib.rs
index a999f7e..d1346e5 100644
--- a/pingora-cache/src/lib.rs
+++ b/pingora-cache/src/lib.rs
@@ -77,6 +77,8 @@ pub enum CachePhase {
Miss,
/// A staled (expired) asset is found
Stale,
+ /// A staled (expired) asset was found, but another request is revalidating it
+ StaleUpdating,
/// A staled (expired) asset was found, so a fresh one was fetched
Expired,
/// A staled (expired) asset was found, and it was revalidated to be fresh
@@ -96,6 +98,7 @@ impl CachePhase {
CachePhase::Hit => "hit",
CachePhase::Miss => "miss",
CachePhase::Stale => "stale",
+ CachePhase::StaleUpdating => "stale-updating",
CachePhase::Expired => "expired",
CachePhase::Revalidated => "revalidated",
CachePhase::RevalidatedNoCache(_) => "revalidated-nocache",
@@ -260,7 +263,7 @@ impl HttpCache {
use CachePhase::*;
match self.phase {
Disabled(_) | Bypass | Miss | Expired | Revalidated | RevalidatedNoCache(_) => true,
- Hit | Stale => false,
+ Hit | Stale | StaleUpdating => false,
Uninit | CacheKey => false, // invalid states for this call, treat them as false to keep it simple
}
}
@@ -509,6 +512,7 @@ impl HttpCache {
match self.phase {
CachePhase::Hit
| CachePhase::Stale
+ | CachePhase::StaleUpdating
| CachePhase::Revalidated
| CachePhase::RevalidatedNoCache(_) => self.inner_mut().body_reader.as_mut().unwrap(),
_ => panic!("wrong phase {:?}", self.phase),
@@ -544,6 +548,7 @@ impl HttpCache {
| CachePhase::Miss
| CachePhase::Expired
| CachePhase::Stale
+ | CachePhase::StaleUpdating
| CachePhase::Revalidated
| CachePhase::RevalidatedNoCache(_) => {
let inner = self.inner_mut();
@@ -786,6 +791,14 @@ impl HttpCache {
// TODO: remove this asset from cache once finished?
}
+ /// Mark this asset as stale, but being updated separately from this request.
+ pub fn set_stale_updating(&mut self) {
+ match self.phase {
+ CachePhase::Stale => self.phase = CachePhase::StaleUpdating,
+ _ => panic!("wrong phase {:?}", self.phase),
+ }
+ }
+
/// Update the variance of the [CacheMeta].
///
/// Note that this process may change the lookup `key`, and eventually (when the asset is
@@ -854,6 +867,7 @@ impl HttpCache {
match self.phase {
// TODO: allow in Bypass phase?
CachePhase::Stale
+ | CachePhase::StaleUpdating
| CachePhase::Expired
| CachePhase::Hit
| CachePhase::Revalidated
@@ -882,6 +896,7 @@ impl HttpCache {
match self.phase {
CachePhase::Miss
| CachePhase::Stale
+ | CachePhase::StaleUpdating
| CachePhase::Expired
| CachePhase::Hit
| CachePhase::Revalidated
@@ -1006,7 +1021,7 @@ impl HttpCache {
/// Whether this request's cache hit is staled
fn has_staled_asset(&self) -> bool {
- self.phase == CachePhase::Stale
+ matches!(self.phase, CachePhase::Stale | CachePhase::StaleUpdating)
}
/// Whether this asset is staled and stale if error is allowed