Skip to content

Instantly share code, notes, and snippets.

@rezan
Last active August 9, 2017 17:46
Show Gist options
  • Save rezan/cd397cde3edb03f9e827be42e07e6c3d to your computer and use it in GitHub Desktop.
Save rezan/cd397cde3edb03f9e827be42e07e6c3d to your computer and use it in GitHub Desktop.
Updating the Last-Modified to capture Edgestash partials
varnishtest "Updating the Last-Modified to capture Edgestash partials"
server s1 {
# Content
rxreq
txresp -hdr "Last-Modified: Wed, 09 Aug 2017 12:00:00 GMT" -body {
This is a response with 2 partials
{{ > /include/p1 }}
{{ > /include/p2 }}
}
expect req.url == "/page"
rxreq
txresp -hdr "Last-Modified: Wed, 09 Aug 2017 16:00:00 GMT" -body "Partial #1"
expect req.url == "/include/p1"
rxreq
txresp -hdr "Last-Modified: Wed, 09 Aug 2017 13:00:00 GMT" -body "Partial #2"
expect req.url == "/include/p2"
# New content (p2)
rxreq
txresp -hdr "Last-Modified: Wed, 09 Aug 2017 12:00:00 GMT" -body {
This is a response with 2 partials
{{ > /include/p1 }}
{{ > /include/p2 }}
}
expect req.url == "/page"
rxreq
txresp -hdr "Last-Modified: Wed, 09 Aug 2017 16:00:00 GMT" -body "Partial #1"
expect req.url == "/include/p1"
rxreq
txresp -hdr "Last-Modified: Wed, 09 Aug 2017 17:00:00 GMT" -body "Partial #3"
expect req.url == "/include/p2"
} -start
varnish v1 -vcl+backend {
import edgestash;
import kvstore;
import std;
sub vcl_init {
# Init kvstore(0) for storing the smallest max-age
kvstore.init(0, 25000);
}
sub vcl_recv {
set req.http.X-top-url = req_top.url;
}
sub vcl_backend_response {
set beresp.ttl = 1s;
set beresp.grace = 0s;
set beresp.keep = 0s;
edgestash.parse_response();
if (kvstore.get(0, bereq.http.X-top-url, "") == "" ||
std.time(beresp.http.Last-Modified, now - 1y) > std.time(kvstore.get(0, bereq.http.X-top-url, ""), now)) {
kvstore.set(0, bereq.http.X-top-url, beresp.http.Last-Modified);
}
}
sub vcl_deliver {
edgestash.execute();
set resp.http.Last-Modified = kvstore.get(0, req.http.X-top-url, resp.http.Last-Modified);
}
} -start
client c1 {
# Normal requests
txreq -url "/page"
rxresp
expect resp.status == 200
expect resp.body ~ "This is a response with 2 partials"
expect resp.body ~ "Partial #1"
expect resp.body ~ "Partial #2"
expect resp.http.Last-Modified == "Wed, 09 Aug 2017 12:00:00 GMT"
txreq -url "/page" -hdr "If-Modified-Since: Wed, 09 Aug 2017 12:00:00 GMT"
rxresp
expect resp.status == 200
expect resp.body ~ "This is a response with 2 partials"
expect resp.body ~ "Partial #1"
expect resp.body ~ "Partial #2"
expect resp.http.Last-Modified == "Wed, 09 Aug 2017 16:00:00 GMT"
txreq -url "/page" -hdr "If-Modified-Since: Wed, 09 Aug 2017 16:00:00 GMT"
rxresp
expect resp.status == 304
delay 2
# IMS Requests coming in when new content exists will still get a 304
txreq -url "/page" -hdr "If-Modified-Since: Wed, 09 Aug 2017 16:00:00 GMT"
rxresp
expect resp.status == 304
# A non IMS response will discover new content
txreq -url "/page"
rxresp
expect resp.status == 200
expect resp.body ~ "This is a response with 2 partials"
expect resp.body ~ "Partial #1"
expect resp.body ~ "Partial #3"
expect resp.http.Last-Modified == "Wed, 09 Aug 2017 16:00:00 GMT"
# New content is now part of IMS
txreq -url "/page" -hdr "If-Modified-Since: Wed, 09 Aug 2017 16:00:00 GMT"
rxresp
expect resp.status == 200
expect resp.body ~ "This is a response with 2 partials"
expect resp.body ~ "Partial #1"
expect resp.body ~ "Partial #3"
expect resp.http.Last-Modified == "Wed, 09 Aug 2017 17:00:00 GMT"
txreq -url "/page" -hdr "If-Modified-Since: Wed, 09 Aug 2017 17:00:00 GMT"
rxresp
expect resp.status == 304
} -run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment