Skip to content

Commit f8b5ab0

Browse files
committed
vcl: Introduce new obj_stale variable
obj_stale variable gives access to the stale object we had in cache when doing a 304 revalidation. It is only readable from vcl_backend_refresh subroutine.
1 parent e6ca62b commit f8b5ab0

File tree

5 files changed

+339
-0
lines changed

5 files changed

+339
-0
lines changed

bin/varnishd/cache/cache_vrt.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ VRT_selecthttp(VRT_CTX, enum gethdr_e where)
257257
case HDR_RESP:
258258
hp = ctx->http_resp;
259259
break;
260+
case HDR_OBJ_STALE:
261+
/* FALLTHROUGH */
260262
case HDR_OBJ:
261263
hp = NULL;
262264
break;
@@ -281,6 +283,13 @@ VRT_GetHdr(VRT_CTX, VCL_HEADER hs)
281283
return (HTTP_GetHdrPack(ctx->req->wrk, ctx->req->objcore,
282284
hs->what));
283285
}
286+
287+
if (hs->where == HDR_OBJ_STALE) {
288+
CHECK_OBJ_NOTNULL(ctx->bo, BUSYOBJ_MAGIC);
289+
CHECK_OBJ_NOTNULL(ctx->bo->stale_oc, OBJCORE_MAGIC);
290+
return (HTTP_GetHdrPack(ctx->bo->wrk, ctx->bo->stale_oc,
291+
hs->what));
292+
}
284293
hp = VRT_selecthttp(ctx, hs->where);
285294
CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
286295
if (!http_GetHdr(hp, hs->what, &p))

bin/varnishd/cache/cache_vrt_var.c

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,42 @@ VRT_r_obj_reason(VRT_CTX)
181181
H__Reason));
182182
}
183183

184+
/*--------------------------------------------------------------------
185+
* stale_oc fields
186+
*/
187+
188+
VCL_INT
189+
VRT_r_obj_stale_status(VRT_CTX)
190+
{
191+
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
192+
CHECK_OBJ_NOTNULL(ctx->bo, BUSYOBJ_MAGIC);
193+
CHECK_OBJ_NOTNULL(ctx->bo->stale_oc, OBJCORE_MAGIC);
194+
195+
return HTTP_GetStatusPack(ctx->bo->wrk ,ctx->bo->stale_oc);
196+
}
197+
198+
VCL_STRING
199+
VRT_r_obj_stale_proto(VRT_CTX)
200+
{
201+
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
202+
CHECK_OBJ_NOTNULL(ctx->bo, BUSYOBJ_MAGIC);
203+
CHECK_OBJ_NOTNULL(ctx->bo->stale_oc, OBJCORE_MAGIC);
204+
205+
return (HTTP_GetHdrPack(ctx->bo->wrk, ctx->bo->stale_oc,
206+
H__Proto));
207+
}
208+
209+
VCL_STRING
210+
VRT_r_obj_stale_reason(VRT_CTX)
211+
{
212+
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
213+
CHECK_OBJ_NOTNULL(ctx->bo, BUSYOBJ_MAGIC);
214+
CHECK_OBJ_NOTNULL(ctx->bo->stale_oc, OBJCORE_MAGIC);
215+
216+
return (HTTP_GetHdrPack(ctx->bo->wrk, ctx->bo->stale_oc,
217+
H__Reason));
218+
}
219+
184220
/*--------------------------------------------------------------------
185221
* beresp bool-fields
186222
*/
@@ -540,6 +576,33 @@ VRT_r_obj_can_esi(VRT_CTX)
540576

541577
/*--------------------------------------------------------------------*/
542578

579+
VCL_STEVEDORE
580+
VRT_r_obj_stale_storage(VRT_CTX)
581+
{
582+
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
583+
CHECK_OBJ_NOTNULL(ctx->bo, BUSYOBJ_MAGIC);
584+
CHECK_OBJ_NOTNULL(ctx->bo->stale_oc, OBJCORE_MAGIC);
585+
AN(ctx->bo->stale_oc->stobj);
586+
CHECK_OBJ_NOTNULL(ctx->bo->stale_oc->stobj->stevedore,
587+
STEVEDORE_MAGIC);
588+
589+
return (ctx->bo->stale_oc->stobj->stevedore);
590+
}
591+
592+
/*--------------------------------------------------------------------*/
593+
594+
VCL_BOOL
595+
VRT_r_obj_stale_can_esi(VRT_CTX)
596+
{
597+
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
598+
CHECK_OBJ_NOTNULL(ctx->bo, BUSYOBJ_MAGIC);
599+
CHECK_OBJ_NOTNULL(ctx->bo->stale_oc, OBJCORE_MAGIC);
600+
601+
return (ObjHasAttr(ctx->bo->wrk, ctx->bo->stale_oc, OA_ESIDATA));
602+
}
603+
604+
/*--------------------------------------------------------------------*/
605+
543606
#define REQ_VAR_L(nm, elem, type, extra) \
544607
\
545608
VCL_VOID \
@@ -765,9 +828,13 @@ VRT_r_##which##_##fld(VRT_CTX) \
765828

766829
/*lint -save -e835 */ // Zero right hand arg to '-'
767830

831+
VRT_DO_EXP_R(obj_stale, ctx->bo->stale_oc, ttl,
832+
ttl_now(ctx) - ctx->bo->stale_oc->t_origin)
768833
VRT_DO_EXP_R(obj, ctx->req->objcore, ttl,
769834
ttl_now(ctx) - ctx->req->objcore->t_origin)
835+
VRT_DO_EXP_R(obj_stale, ctx->bo->stale_oc, grace, 0)
770836
VRT_DO_EXP_R(obj, ctx->req->objcore, grace, 0)
837+
VRT_DO_EXP_R(obj_stale, ctx->bo->stale_oc, keep, 0)
771838
VRT_DO_EXP_R(obj, ctx->req->objcore, keep, 0)
772839
VRT_DO_EXP_L(beresp, ctx->bo->fetch_objcore, ttl,
773840
ttl_now(ctx) - ctx->bo->fetch_objcore->t_origin)
@@ -799,6 +866,7 @@ VRT_DO_TIME_R(resp, req, t_resp)
799866
VRT_DO_TIME_R(bereq, bo, t_first)
800867
VRT_DO_TIME_R(beresp, bo, t_resp)
801868
VRT_DO_TIME_R(obj, req->objcore, t_origin)
869+
VRT_DO_TIME_R(obj_stale, bo->stale_oc, t_origin)
802870

803871
/*--------------------------------------------------------------------
804872
*/
@@ -813,6 +881,7 @@ VRT_r_##which##_##age(VRT_CTX) \
813881
return (ttl_now(ctx) - oc->t_origin); \
814882
}
815883

884+
VRT_DO_AGE_R(obj_stale, ctx->bo->stale_oc)
816885
VRT_DO_AGE_R(obj, ctx->req->objcore)
817886
VRT_DO_AGE_R(beresp, ctx->bo->fetch_objcore)
818887

@@ -986,6 +1055,28 @@ VRT_r_obj_uncacheable(VRT_CTX)
9861055

9871056
/*--------------------------------------------------------------------*/
9881057

1058+
VCL_INT
1059+
VRT_r_obj_stale_hits(VRT_CTX)
1060+
{
1061+
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
1062+
CHECK_OBJ_NOTNULL(ctx->bo, BUSYOBJ_MAGIC);
1063+
CHECK_OBJ_NOTNULL(ctx->bo->stale_oc, OBJCORE_MAGIC);
1064+
1065+
return (ctx->bo->stale_oc->hits);
1066+
}
1067+
1068+
VCL_BOOL
1069+
VRT_r_obj_stale_uncacheable(VRT_CTX)
1070+
{
1071+
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
1072+
CHECK_OBJ_NOTNULL(ctx->bo, BUSYOBJ_MAGIC);
1073+
CHECK_OBJ_NOTNULL(ctx->bo->stale_oc, OBJCORE_MAGIC);
1074+
1075+
return (ctx->bo->stale_oc->flags & OC_F_HFM ? 1 : 0);
1076+
}
1077+
1078+
/*--------------------------------------------------------------------*/
1079+
9891080
VCL_BOOL
9901081
VRT_r_resp_is_streaming(VRT_CTX)
9911082
{

bin/varnishtest/tests/b00083.vtc

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
varnishtest "Test obj_stale vcl variables"
2+
3+
server s1 {
4+
rxreq
5+
txresp -hdr "Etag: abcd" -hdr "from-bo: true" -bodylen 10
6+
rxreq
7+
expect req.http.if-none-match == "abcd"
8+
txresp -status 304
9+
} -start
10+
11+
varnish v1 -vcl+backend {
12+
13+
sub vcl_backend_response {
14+
set beresp.http.vbresp = "true";
15+
set beresp.ttl = 0.01s;
16+
set beresp.grace = 0s;
17+
set beresp.keep = 10m;
18+
set beresp.http.was-304 = beresp.was_304;
19+
}
20+
21+
sub vcl_backend_refresh {
22+
set beresp.http.vbref = "true";
23+
24+
set beresp.http.http = obj_stale.http.from-bo;
25+
set beresp.http.age = obj_stale.age;
26+
set beresp.http.can_esi = obj_stale.can_esi;
27+
set beresp.http.grace = obj_stale.grace;
28+
set beresp.http.hits = obj_stale.hits;
29+
set beresp.http.keep = obj_stale.keep;
30+
set beresp.http.proto = obj_stale.proto;
31+
set beresp.http.reason = obj_stale.reason;
32+
set beresp.http.status = obj_stale.status;
33+
set beresp.http.storage = obj_stale.storage;
34+
set beresp.http.time = obj_stale.time;
35+
set beresp.http.ttl = obj_stale.ttl;
36+
set beresp.http.uncacheable = obj_stale.uncacheable;
37+
38+
return (merge);
39+
}
40+
} -start
41+
42+
client c1 {
43+
txreq
44+
rxresp
45+
expect resp.status == 200
46+
47+
expect resp.http.was-304 == false
48+
expect resp.http.vbref == <undef>
49+
expect resp.http.vbresp == true
50+
expect resp.http.from-bo == true
51+
} -run
52+
53+
delay 0.01
54+
55+
client c2 {
56+
txreq
57+
rxresp
58+
expect resp.status == 200
59+
expect resp.http.was-304 == true
60+
expect resp.http.vbresp == true
61+
expect resp.http.vbref == true
62+
expect resp.http.from-bo == true
63+
64+
expect resp.http.http == true
65+
expect resp.http.age == 0
66+
expect resp.http.can_esi == false
67+
expect resp.http.grace == 0.000
68+
expect resp.http.hits == 0
69+
expect resp.http.keep == 600.000
70+
expect resp.http.proto == HTTP/1.1
71+
expect resp.http.reason == OK
72+
expect resp.http.status == 200
73+
expect resp.http.storage == storage.s0
74+
expect resp.http.time != <undef>
75+
expect resp.http.ttl != <undef>
76+
expect resp.http.uncacheable == false
77+
} -run

0 commit comments

Comments
 (0)