Created
November 17, 2017 13:11
-
-
Save rezan/864f8f97400b75500f505e3e871ee9ce to your computer and use it in GitHub Desktop.
Do not update an object which is older than whats in cache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
varnishtest "Do not cache stale Last Modified" | |
server s1 { | |
rxreq | |
txresp -hdr "Last-Modified: Wed, 27 Apr 2016 14:05:52 GMT" -hdr "Version: 1" | |
rxreq | |
txresp -hdr "Last-Modified: Wed, 27 Apr 2016 16:05:52 GMT" -hdr "Version: 2" | |
rxreq | |
txresp -hdr "Last-Modified: Wed, 27 Apr 2016 14:05:52 GMT" -hdr "Version: 3" | |
accept | |
rxreq | |
txresp -hdr "Last-Modified: Wed, 27 Apr 2016 14:05:52 GMT" -hdr "Version: 4" | |
accept | |
rxreq | |
txresp -hdr "Last-Modified: Wed, 27 Apr 2016 18:05:52 GMT" -hdr "Version: 5" | |
} -start | |
varnish v1 -vcl+backend { | |
import std; | |
sub vcl_hit { | |
if (obj.http.Last-Modified) { | |
set req.http.X-hit-Last-Modified = obj.http.Last-Modified; | |
} | |
if (req.http.X-serve-stale == "true") { | |
set req.http.X-abandon = "true"; | |
return(deliver); | |
} | |
set req.http.X-serve-stale = "possible"; | |
if (std.healthy(req.backend_hint) && obj.ttl < 0s) { | |
return(fetch); | |
} | |
} | |
sub vcl_backend_fetch { | |
if (bereq.http.X-abandon == "true") { | |
return(abandon); | |
} | |
} | |
sub vcl_backend_response { | |
set beresp.ttl = 1s; | |
set beresp.grace = 1d; | |
// Stale object has a more recent Last-Modified, do not cache the update | |
if (std.time(bereq.http.X-hit-Last-Modified, now - 30y) > std.time(beresp.http.Last-Modified, now)) { | |
return(abandon); | |
} | |
} | |
sub vcl_backend_error { | |
if (bereq.http.X-serve-stale == "possible") { | |
return (abandon); | |
} | |
} | |
sub vcl_synth { | |
if (resp.status == 503 && req.http.X-serve-stale == "possible") { | |
set req.http.X-serve-stale = "true"; | |
unset req.http.X-hit-Last-Modified; | |
return(restart); | |
} | |
} | |
sub vcl_deliver { | |
set resp.http.X-hits = obj.hits; | |
} | |
} -start | |
client c1 { | |
# Version 1 is good | |
txreq -hdr "Request: 1" | |
rxresp | |
expect resp.http.Version == "1" | |
expect resp.http.X-hits == "0" | |
txreq -hdr "Request: 2" | |
rxresp | |
expect resp.http.Version == "1" | |
expect resp.http.X-hits == "1" | |
delay 1.5 | |
# Version 2 is good | |
txreq -hdr "Request: 3" | |
rxresp | |
expect resp.http.Version == "2" | |
expect resp.http.X-hits == "0" | |
txreq -hdr "Request: 4" | |
rxresp | |
expect resp.http.Version == "2" | |
expect resp.http.X-hits == "1" | |
delay 1.5 | |
# Version 3 is old | |
txreq -hdr "Request: 5" | |
rxresp | |
expect resp.http.Version == "2" | |
expect resp.http.X-hits == "3" | |
# Version 4 is old | |
txreq -hdr "Request: 6" | |
rxresp | |
expect resp.http.Version == "2" | |
expect resp.http.X-hits == "5" | |
# Version 5 is good | |
txreq -hdr "Request: 7" | |
rxresp | |
expect resp.http.Version == "5" | |
expect resp.http.X-hits == "0" | |
txreq -hdr "Request: 8" | |
rxresp | |
expect resp.http.Version == "5" | |
expect resp.http.X-hits == "1" | |
} -run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment