Skip to content

Commit 39a9dae

Browse files
committed
Add std.rfc_ttl()
From the added documentation: Re-calculate the object timers (``beresp.ttl``, ``beresp.grace`` and ``beresp.keep``) based on the current state of ``beresp`` as if it had been processed by core code before ``vcl_backend_response`` was called. This does not change ``beresp.uncacheable``. This is useful to get the default ttl calculations after modifications of relevant properties like ``beresp.status``, ``beresp.http.Date``, ``beresp.http.Age`` or ``beresp.http.Cache-Control``. Documentation details polished by SashankBhamidi, thank you.
1 parent 6e4e674 commit 39a9dae

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

vmod/tests/std_b00015.vtc

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
varnishtest "std.rfc_ttl()"
2+
3+
server s1 {
4+
rxreq
5+
txresp -status 202 -hdr "Cache-Control: public, s-maxage=4711"
6+
7+
rxreq
8+
expect req.url == "/200"
9+
txresp -status 200 \
10+
-hdr "Cache-Control: public, s-maxage=0, stale-while-revalidate=99" \
11+
-hdr {Etag: "foo"} \
12+
-bodylen 64
13+
14+
rxreq
15+
expect req.url == "/200"
16+
expect req.http.If-None-Match == {"foo"}
17+
txresp -status 304 -hdr "Cache-Control: s-maxage=11, stale-while-revalidate=77"
18+
} -start
19+
20+
varnish v1 -vcl+backend {
21+
import std;
22+
23+
sub vcl_backend_refresh {
24+
set beresp.http.Cache-Control = "s-maxage=10, stale-while-revalidate=42";
25+
std.rfc_ttl();
26+
}
27+
28+
sub vcl_backend_response {
29+
if (beresp.status == 202) {
30+
set beresp.status = 200;
31+
std.rfc_ttl();
32+
set beresp.status = 202;
33+
}
34+
# avoid zero ttl which is uncacheable
35+
set beresp.ttl += 0.001s;
36+
}
37+
} -start
38+
39+
logexpect l1 -v v1 -g vxid -q "vxid == 1002" {
40+
fail add * Error
41+
fail add * VCL_Error
42+
fail add * End
43+
expect * 1002 TTL {^RFC -1 }
44+
expect 1 = BerespStatus {^200}
45+
expect 1 = TTL {^RFC 4711 }
46+
fail clear
47+
} -start
48+
49+
logexpect l2 -v v1 -g vxid -q "vxid == 1004" {
50+
fail add * Error
51+
fail add * VCL_Error
52+
fail add * End
53+
expect * 1004 BereqURL {^/200}
54+
expect * = BerespStatus {^200}
55+
expect * = TTL {^RFC 0 99 }
56+
fail clear
57+
} -start
58+
59+
logexpect l3 -v v1 -g vxid -q "vxid == 1006" {
60+
fail add * Error
61+
fail add * VCL_Error
62+
fail add * End
63+
expect * 1006 BereqURL {^/200}
64+
expect * = BerespStatus {^304}
65+
expect * = TTL {^RFC 11 77 }
66+
expect * = TTL {^RFC 10 42 }
67+
fail clear
68+
} -start
69+
70+
71+
client c1 {
72+
txreq
73+
rxresp
74+
expect resp.status == 202
75+
76+
txreq -url "/200"
77+
rxresp
78+
expect resp.status == 200
79+
80+
txreq -url "/200"
81+
rxresp
82+
expect resp.status == 200
83+
} -run
84+
85+
logexpect l1 -wait
86+
logexpect l2 -wait
87+
logexpect l3 -wait

vmod/vmod_std.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,3 +385,23 @@ vmod_timed_call(VRT_CTX, VCL_SUB sub)
385385
VRT_call(ctx, sub);
386386
return (VTIM_mono() - b);
387387
}
388+
389+
VCL_VOID v_matchproto_(td_std_rfc_ttl)
390+
vmod_rfc_ttl(VRT_CTX)
391+
{
392+
struct busyobj *bo;
393+
struct objcore *oc;
394+
395+
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
396+
// $Restrict guarantees
397+
bo = ctx->bo;
398+
CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
399+
oc = bo->fetch_objcore;
400+
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
401+
402+
RFC2616_Ttl(bo, ctx->now,
403+
&oc->t_origin,
404+
&oc->ttl,
405+
&oc->grace,
406+
&oc->keep);
407+
}

vmod/vmod_std.vcc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,19 @@ $Function DURATION timed_call(SUB)
696696
Call the given SUB and return a high precision measurement of the
697697
execution time.
698698

699+
$Function VOID rfc_ttl()
700+
701+
$Restrict vcl_backend_response vcl_backend_refresh
702+
703+
Re-calculate the object timers (``beresp.ttl``, ``beresp.grace`` and
704+
``beresp.keep``) based on the current state of ``beresp`` as if it had been
705+
processed by core code before ``vcl_backend_response`` was called. This does not
706+
change ``beresp.uncacheable``.
707+
708+
This is useful to get the default ttl calculations after modifications of
709+
relevant properties like ``beresp.status``, ``beresp.http.Date``,
710+
``beresp.http.Age`` or ``beresp.http.Cache-Control``.
711+
699712
SEE ALSO
700713
========
701714

0 commit comments

Comments
 (0)