Skip to content
This repository was archived by the owner on Feb 16, 2026. It is now read-only.

Commit ecf9223

Browse files
committed
vrt_var: Make req.ttl and req.grace unsettable
req.ttl and req.grace are NAN when unset
1 parent a131c45 commit ecf9223

6 files changed

Lines changed: 66 additions & 7 deletions

File tree

bin/varnishd/cache/cache_expire.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,10 @@ EXP_Ttl(const struct req *req, const struct objcore *oc)
7373
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
7474

7575
r = oc->ttl;
76-
if (req != NULL && req->d_ttl >= 0. && req->d_ttl < r)
76+
if (req != NULL && !isnan(req->d_ttl) && req->d_ttl < r) {
77+
assert(req->d_ttl >= 0.);
7778
r = req->d_ttl;
79+
}
7880
return (oc->t_origin + r);
7981
}
8082

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

9395
g = oc->grace;
94-
if (req != NULL && req->d_grace >= 0. && req->d_grace < g)
96+
if (req != NULL && !isnan(req->d_grace) && req->d_grace < g) {
97+
assert(req->d_grace >= 0.);
9598
g = req->d_grace;
99+
}
96100
return (EXP_Ttl(req, oc) + g);
97101
}
98102

bin/varnishd/cache/cache_req_fsm.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -893,8 +893,8 @@ cnt_recv_prep(struct req *req, const char *ci)
893893
VRT_Assign_Backend(&req->director_hint,
894894
VCL_DefaultDirector(req->vcl));
895895

896-
req->d_ttl = -1;
897-
req->d_grace = -1;
896+
req->d_ttl = NAN;
897+
req->d_grace = NAN;
898898
req->disable_esi = 0;
899899
req->hash_always_miss = 0;
900900
req->hash_ignore_busy = 0;

bin/varnishd/cache/cache_vrt_var.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,12 +576,24 @@ VRT_r_req_##nm(VRT_CTX) \
576576
return (ctx->req->elem); \
577577
}
578578

579+
#define REQ_VAR_U(nm, elem, val) \
580+
\
581+
VCL_VOID \
582+
VRT_u_req_##nm(VRT_CTX) \
583+
{ \
584+
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); \
585+
CHECK_OBJ_NOTNULL(ctx->req, REQ_MAGIC); \
586+
ctx->req->elem = val; \
587+
}
588+
579589
REQ_VAR_R(backend_hint, director_hint, VCL_BACKEND)
580590

581591
REQ_VAR_L(ttl, d_ttl, VCL_DURATION, if (!(arg>0.0)) arg = 0;)
582592
REQ_VAR_R(ttl, d_ttl, VCL_DURATION)
593+
REQ_VAR_U(ttl, d_ttl, NAN)
583594
REQ_VAR_L(grace, d_grace, VCL_DURATION, if (!(arg>0.0)) arg = 0;)
584595
REQ_VAR_R(grace, d_grace, VCL_DURATION)
596+
REQ_VAR_U(grace, d_grace, NAN)
585597

586598
VCL_VOID
587599
VRT_l_req_backend_hint(VRT_CTX, VCL_BACKEND be)

bin/varnishtest/tests/b00064.vtc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ client c2 {
8484
expect resp.status == 200
8585
expect resp.body == "1"
8686
expect resp.http.X-grace == "60.000"
87-
expect resp.http.X-req-grace < 0.
87+
expect resp.http.X-req-grace == "NAN"
8888
expect resp.http.X-was-bgfetch == <undef>
8989
} -run
9090

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

bin/varnishtest/tests/b00093.vtc

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
varnishtest "unset req.ttl and req.grace"
2+
3+
server s1 {
4+
rxreq
5+
expect req.http.ttl-initial == "NAN"
6+
expect req.http.grace-initial == "NAN"
7+
expect req.http.ttl-set == 0.000
8+
expect req.http.grace-set == 0.000
9+
expect req.http.ttl-unset == "NAN"
10+
expect req.http.grace-unset == "NAN"
11+
txresp
12+
} -start
13+
14+
varnish v1 -vcl+backend {
15+
sub vcl_recv {
16+
set req.http.ttl-initial = req.ttl;
17+
set req.http.grace-initial = req.grace;
18+
19+
set req.ttl = 0s;
20+
set req.grace = 0s;
21+
22+
set req.http.ttl-set = req.ttl;
23+
set req.http.grace-set = req.grace;
24+
25+
unset req.ttl;
26+
unset req.grace;
27+
28+
set req.http.ttl-unset = req.ttl;
29+
set req.http.grace-unset = req.grace;
30+
}
31+
} -start
32+
33+
client c1 -repeat 2 {
34+
txreq
35+
rxresp
36+
expect resp.status == 200
37+
} -run

doc/sphinx/reference/vcl_var.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,14 @@ req.grace
272272

273273
Writable from: client
274274

275+
Unsettable from: client
275276

276277
Upper limit on the object grace.
277278

278279
During lookup the minimum of req.grace and the object's stored
279-
grace value will be used as the object's grace.
280+
grace value will be used as the object's grace. Setting req.grace
281+
to 0s prevents hits on stale objects. Negative values are treated
282+
as 0s.
280283

281284

282285
.. _req.hash:
@@ -536,8 +539,11 @@ req.ttl
536539

537540
Writable from: client
538541

542+
Unsettable from: client
539543

540544
Upper limit on the object age for cache lookups to return hit.
545+
Lookups always miss when req.ttl is set to 0s. Negative values
546+
are treated as 0s.
541547

542548

543549
.. _req.url:

0 commit comments

Comments
 (0)