Updated patch after commit f2caddcf.
On Thu, Aug 28, 2014 at 6:14 PM, Federico Schwindt <[email protected]> wrote:
> You meant vcl_backend_response, right?
>
> Actually there is yet another reason to do it in C.
>
> If we were going to do it in the builtin vcl people wanting to override
> this value would need to either return early or fiddle with the header.
>
>
> On Thu, Aug 28, 2014 at 7:26 AM, Nils Goroll <[email protected]> wrote:
>
>> On 27/08/14 23:34, Geoff Simmons wrote:
>> > I think I'm unsure about what we're striving for in Varnish 4 --
>> > wasn't the goal to move as much caching policy as possible out to VCL,
>> > with good defaults in builtin.vcl?
>>
>> I see a bit of a tendency that we are moving towards having C code provide
>> good/better defaults, still allowing VCL to modify them.
>>
>> This definitely is the case with fgs' proposed patch, vcl_backend_fetch
>> still
>> has the final word.
>>
>> But, yes, s-w-r can be done in VCL already (and it really is a good
>> question if
>> we shold just add it to the builtin.vcl). s-i-e, I think, needs
>> additional C
>> support to allow for a VCL implementation (see my post "restarting for bad
>> synchronous responses").
>>
>> <side_note>
>> Some header mangling (Vary, Etag) we are doing in fetch processors at the
>> moment
>> is the exact contrary - VCL control is reduced (limited to vcl_deliver)
>> until we
>> get explicit fetch processor pushes (which phk is planning for).
>> </side_note>
>>
>> Nils
>>
>
>
diff --git a/bin/varnishd/cache/cache.h b/bin/varnishd/cache/cache.h
index 08e8714..e94745c 100644
--- a/bin/varnishd/cache/cache.h
+++ b/bin/varnishd/cache/cache.h
@@ -1243,6 +1243,7 @@ unsigned RFC2616_Req_Gzip(const struct http *);
int RFC2616_Do_Cond(const struct req *sp);
void RFC2616_Weaken_Etag(struct http *hp);
void RFC2616_Vary_AE(struct http *hp);
+void RFC5861_Stale(struct busyobj *);
/* stevedore.c */
int STV_NewObject(struct busyobj *, const char *hint, unsigned len);
diff --git a/bin/varnishd/cache/cache_fetch.c b/bin/varnishd/cache/cache_fetch.c
index 001cd79..b1dd882 100644
--- a/bin/varnishd/cache/cache_fetch.c
+++ b/bin/varnishd/cache/cache_fetch.c
@@ -331,6 +331,8 @@ vbf_stp_startfetch(struct worker *wrk, struct busyobj *bo)
EXP_Clr(&bo->fetch_objcore->exp);
RFC2616_Ttl(bo, now);
+ RFC5861_Stale(bo);
+
/* private objects have negative TTL */
if (bo->fetch_objcore->flags & OC_F_PRIVATE)
bo->fetch_objcore->exp.ttl = -1.;
diff --git a/bin/varnishd/cache/cache_rfc2616.c b/bin/varnishd/cache/cache_rfc2616.c
index b7737bc..4a8957f 100644
--- a/bin/varnishd/cache/cache_rfc2616.c
+++ b/bin/varnishd/cache/cache_rfc2616.c
@@ -404,3 +404,35 @@ RFC2616_Vary_AE(struct http *hp)
http_SetHeader(hp, "Vary: Accept-Encoding");
}
}
+
+/*
+ * RFC5861 outlines a way to control the use of stale responses.
+ * We use this to initialize the grace period.
+ */
+
+void
+RFC5861_Stale(struct busyobj *bo)
+{
+ char *p;
+ const struct http *hp;
+ struct exp *expp;
+
+ expp = &bo->fetch_objcore->exp;
+
+ /*
+ * If we are not meant to cache this ignore any potential
+ * stale-while-revalidate values.
+ */
+ if (expp->ttl <= 0.)
+ return;
+
+ hp = bo->beresp;
+
+ if (http_GetHdrField(hp, H_Cache_Control,
+ "stale-while-revalidate", &p) && p != NULL) {
+ if (*p == '-')
+ expp->grace = 0;
+ else
+ expp->grace = strtoul(p, NULL, 0);
+ }
+}
diff --git a/bin/varnishtest/tests/b00043.vtc b/bin/varnishtest/tests/b00043.vtc
new file mode 100644
index 0000000..efbc03a
--- /dev/null
+++ b/bin/varnishtest/tests/b00043.vtc
@@ -0,0 +1,41 @@
+varnishtest "Test stale-while-revalidate"
+
+server s1 {
+ rxreq
+ txresp -hdr "Cache-Control: max-age=30, stale-while-revalidate=30"
+ rxreq
+ txresp -hdr "Cache-Control: max-age=0, stale-while-revalidate=30"
+ rxreq
+ txresp -hdr "Cache-Control: max-age=30, stale-while-revalidate=30" -hdr "Age: 40"
+ rxreq
+ txresp -status 500 -hdr "Cache-Control: max-age=30, stale-while-revalidate=30"
+} -start
+
+varnish v1 -vcl+backend {
+ sub vcl_backend_response {
+ set beresp.http.grace = beresp.grace;
+ set beresp.http.ttl = beresp.ttl;
+ }
+} -start
+
+client c1 {
+ txreq -url /1
+ rxresp
+ expect resp.http.grace == 30.000
+ expect resp.http.ttl == 30.000
+
+ txreq -url /2
+ rxresp
+ expect resp.http.grace == 10.000
+ expect resp.http.ttl == 0.000
+
+ txreq -url /3
+ rxresp
+ expect resp.http.grace == 30.000
+ expect resp.http.ttl == -10.000
+
+ txreq -url /4
+ rxresp
+ expect resp.http.grace == 10.000
+ expect resp.http.ttl == 0.000
+} -run
_______________________________________________
varnish-dev mailing list
[email protected]
https://www.varnish-cache.org/lists/mailman/listinfo/varnish-dev