Skip to content

Instantly share code, notes, and snippets.

@perbu
Created March 1, 2016 18:16
Show Gist options
  • Save perbu/93803707dbcdbc345da0 to your computer and use it in GitHub Desktop.
Save perbu/93803707dbcdbc345da0 to your computer and use it in GitHub Desktop.
sub vcl_hit {
if (obj.ttl >= 0s) {
# normal hit
return (deliver);
}
# We have no fresh fish. Lets look at the stale ones.
if (std.healthy(req.backend_hint)) {
# Backend is healthy. Limit age to 10s.
if (obj.ttl + 10s > 0s) {
set req.http.grace = "normal(limited)";
return (deliver);
} else {
# No candidate for grace. Fetch a fresh object.
return(fetch);
}
} else {
# backend is sick - use full grace
if (obj.ttl + obj.grace > 0s) {
set req.http.grace = "full";
return (deliver);
} else {
# no graced object.
return (fetch);
}
}
}
sub vcl_backend_response {
set beresp.ttl = 10s;
set beresp.grace = 1h;
}
sub vcl_recv {
# intial state
set req.http.grace = "none";
}
sub vcl_deliver {
# copy to resp so we can tell from the outside.
set resp.http.grace = req.http.grace;
}
@dancriel
Copy link

dancriel commented Mar 4, 2017

return (fetch); should be replaced with return (miss); if using Varnish 5.0

@dancriel
Copy link

dancriel commented Mar 4, 2017

@arikogan return(hash) is called at the end of the default vcl_recv function and is not needed in this vcl, unless you want to prevent the default vcl_recv behavior for some reason

@ureimers
Copy link

@dancriel I'm running Varnish 5.0.0 and without return (hash) at the end of my altered vcl_recv, Varnish won't return cached results. When calling /etc/init.d/varnish configtest varnish returns the built-in configuration as well which includes this note:

NB! You do NOT need to copy & paste all of these functions into your"
" * own vcl code, if you do not provide a definition of one of these"
" * functions, the compiler will automatically fall back to the default"
" * code from this file."

This sounds like "as long as you don't define vcl_recv the built-in version will be used but if you redefine it, you should copy 'n paste it and add your custom code".

@dancriel
Copy link

@ureimers My understanding is that the default behavior will run after your custom behavior, unless you return somewhere in your vcl_recv function, which will prevent the default behavior (not recommended). Hopefully someone can correct me if this is wrong.

@alekstrust
Copy link

Is this working on Varnish 6? I never hit the req.http.grace = "full"; line. After 10 seconds of the backend going offline, I get a 503 Backend fetch failed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment