Skip to content

Commit

Permalink
vrt_var: Make req.ttl and req.grace unsettable
Browse files Browse the repository at this point in the history
req.ttl and req.grace are NAN when unset
  • Loading branch information
AlveElde committed Oct 9, 2024
1 parent 596d08a commit b8a4e18
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 7 deletions.
8 changes: 6 additions & 2 deletions bin/varnishd/cache/cache_expire.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ EXP_Ttl(const struct req *req, const struct objcore *oc)
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);

r = oc->ttl;
if (req != NULL && req->d_ttl >= 0. && req->d_ttl < r)
if (req != NULL && !isnan(req->d_ttl) && req->d_ttl < r) {
assert(req->d_ttl >= 0.);
r = req->d_ttl;
}
return (oc->t_origin + r);
}

Expand All @@ -91,8 +93,10 @@ EXP_Ttl_grace(const struct req *req, const struct objcore *oc)
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);

g = oc->grace;
if (req != NULL && req->d_grace >= 0. && req->d_grace < g)
if (req != NULL && !isnan(req->d_grace) && req->d_grace < g) {
assert(req->d_grace >= 0.);
g = req->d_grace;
}
return (EXP_Ttl(req, oc) + g);
}

Expand Down
4 changes: 2 additions & 2 deletions bin/varnishd/cache/cache_req_fsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -893,8 +893,8 @@ cnt_recv_prep(struct req *req, const char *ci)
VRT_Assign_Backend(&req->director_hint,
VCL_DefaultDirector(req->vcl));

req->d_ttl = -1;
req->d_grace = -1;
req->d_ttl = NAN;
req->d_grace = NAN;
req->disable_esi = 0;
req->hash_always_miss = 0;
req->hash_ignore_busy = 0;
Expand Down
12 changes: 12 additions & 0 deletions bin/varnishd/cache/cache_vrt_var.c
Original file line number Diff line number Diff line change
Expand Up @@ -576,12 +576,24 @@ VRT_r_req_##nm(VRT_CTX) \
return (ctx->req->elem); \
}

#define REQ_VAR_U(nm, elem, val) \
\
VCL_VOID \
VRT_u_req_##nm(VRT_CTX) \
{ \
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); \
CHECK_OBJ_NOTNULL(ctx->req, REQ_MAGIC); \
ctx->req->elem = val; \
}

REQ_VAR_R(backend_hint, director_hint, VCL_BACKEND)

REQ_VAR_L(ttl, d_ttl, VCL_DURATION, if (!(arg>0.0)) arg = 0;)
REQ_VAR_R(ttl, d_ttl, VCL_DURATION)
REQ_VAR_U(ttl, d_ttl, NAN)
REQ_VAR_L(grace, d_grace, VCL_DURATION, if (!(arg>0.0)) arg = 0;)
REQ_VAR_R(grace, d_grace, VCL_DURATION)
REQ_VAR_U(grace, d_grace, NAN)

VCL_VOID
VRT_l_req_backend_hint(VRT_CTX, VCL_BACKEND be)
Expand Down
4 changes: 2 additions & 2 deletions bin/varnishtest/tests/b00064.vtc
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ client c2 {
expect resp.status == 200
expect resp.body == "1"
expect resp.http.X-grace == "60.000"
expect resp.http.X-req-grace < 0.
expect resp.http.X-req-grace == "NAN"
expect resp.http.X-was-bgfetch == <undef>
} -run

Expand Down Expand Up @@ -118,7 +118,7 @@ client c4 {
# We should get what c1 got in the very beginning
expect resp.body == "1"
expect resp.http.X-grace == "60.000"
expect resp.http.X-req-grace < 0.
expect resp.http.X-req-grace == "NAN"
expect resp.http.X-was-bgfetch == <undef>
} -start

Expand Down
37 changes: 37 additions & 0 deletions bin/varnishtest/tests/b00093.vtc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
varnishtest "unset req.ttl and req.grace"

server s1 {
rxreq
expect req.http.ttl-initial == "NAN"
expect req.http.grace-initial == "NAN"
expect req.http.ttl-set == 0.000
expect req.http.grace-set == 0.000
expect req.http.ttl-unset == "NAN"
expect req.http.grace-unset == "NAN"
txresp
} -start

varnish v1 -vcl+backend {
sub vcl_recv {
set req.http.ttl-initial = req.ttl;
set req.http.grace-initial = req.grace;

set req.ttl = 0s;
set req.grace = 0s;

set req.http.ttl-set = req.ttl;
set req.http.grace-set = req.grace;

unset req.ttl;
unset req.grace;

set req.http.ttl-unset = req.ttl;
set req.http.grace-unset = req.grace;
}
} -start

client c1 -repeat 2 {
txreq
rxresp
expect resp.status == 200
} -run
10 changes: 9 additions & 1 deletion doc/sphinx/reference/vcl_var.rst
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,14 @@ req.grace

Writable from: client

Unsettable from: client

Upper limit on the object grace.

During lookup the minimum of req.grace and the object's stored
grace value will be used as the object's grace.
grace value will be used as the object's grace. Setting req.grace
to 0s prevents hits on stale objects. Negative values are treated
as 0s.


.. _req.hash:
Expand Down Expand Up @@ -536,8 +539,13 @@ req.ttl

Writable from: client

Unsettable from: client

Upper limit on the object age for cache lookups to return hit.
Setting ``req.ttl = 0s`` forces a cache MISS, but unlike
``req.hash_always_miss = true``, the request may go on the
waitinglist for an ongoing fetch. Negative values are treated
as 0s.


.. _req.url:
Expand Down

0 comments on commit b8a4e18

Please sign in to comment.