[master] 9c0abf0c8 expire: Introduce EXP_Reduce()

Nils Goroll nils.goroll at uplex.de
Mon Mar 25 14:56:07 UTC 2024


commit 9c0abf0c8065c2dab42410475346e7dff2156bc7
Author: AlveElde <alve_elde at hotmail.com>
Date:   Fri Mar 22 11:21:46 2024 +0100

    expire: Introduce EXP_Reduce()
    
    This commit introduces EXP_Reduce(), a function to reduce object timers.
    The goal is to provide a function better suited to soft-purging objects,
    as EXP_Rearm() has some non-obvious disadvantages when used or this
    purpose.
    
    When EXP_Rearm() is used to soft-purge an object by setting its TTL to
    0, the expiry is effectively reset to the start of the objects grace
    period. This happens because the object TTL includes a time delta
    between object insertion time (oc->t_origin) and now. The result is that
    a soft-purge extends the lifetime of an already stale object, and
    repeated soft-purges can keep the object away from expiry indefinitely.
    
    The EXP_Reduce() function is better suited for soft-purging, as it only
    updates an objects TTL if it would reduce the objects lifetime. The
    function also has facilities for reducing grace and keep.

diff --git a/bin/varnishd/cache/cache_expire.c b/bin/varnishd/cache/cache_expire.c
index fb823e3cc..eb862bf49 100644
--- a/bin/varnishd/cache/cache_expire.c
+++ b/bin/varnishd/cache/cache_expire.c
@@ -219,6 +219,28 @@ EXP_Insert(struct worker *wrk, struct objcore *oc)
 	}
 }
 
+/*--------------------------------------------------------------------
+ * Reduce object timers
+ */
+
+void
+EXP_Reduce(struct objcore *oc, vtim_real now,
+    vtim_dur ttl, vtim_dur grace, vtim_dur keep)
+{
+
+	CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
+	assert(oc->refcnt > 0);
+
+	if (!isnan(ttl) && now + ttl - oc->t_origin >= oc->ttl)
+		ttl = NAN;
+	if (!isnan(grace) && grace >= oc->grace)
+		grace = NAN;
+	if (!isnan(keep) && keep >= oc->keep)
+		keep = NAN;
+
+	EXP_Rearm(oc, now, ttl, grace, keep);
+}
+
 /*--------------------------------------------------------------------
  * We have changed one or more of the object timers, tell the exp_thread
  *
diff --git a/bin/varnishd/cache/cache_varnishd.h b/bin/varnishd/cache/cache_varnishd.h
index d759f95e7..61d3145c9 100644
--- a/bin/varnishd/cache/cache_varnishd.h
+++ b/bin/varnishd/cache/cache_varnishd.h
@@ -240,6 +240,8 @@ void EXP_Remove(struct objcore *, const struct objcore *);
 /* cache_exp.c */
 void EXP_Rearm(struct objcore *oc, vtim_real now,
     vtim_dur ttl, vtim_dur grace, vtim_dur keep);
+void EXP_Reduce(struct objcore *oc, vtim_real now,
+    vtim_dur ttl, vtim_dur grace, vtim_dur keep);
 
 /* From cache_main.c */
 void BAN_Init(void);


More information about the varnish-commit mailing list