Skip to content

Instantly share code, notes, and snippets.

@cosimo
Last active April 11, 2018 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cosimo/6d3318bf173357dfb2652b7b2e81e1e0 to your computer and use it in GitHub Desktop.
Save cosimo/6d3318bf173357dfb2652b7b2e81e1e0 to your computer and use it in GitHub Desktop.
vcl 4.0;
import std;
import vsthrottle;
sub vcl_recv {
...
call rate_limit;
...
}
#
# Throttling based on request inspection
#
sub rate_limit {
if (req.url ~ "pattern1") {
std.log("pattern1 requests must never be throttled");
return; # <---- Need to return early here, but can't do it
}
if (req.url ~ "pattern2") {
if (vsthrottle.is_denied("pattern2" + client.identity, 100, 10s)) {
std.log("pattern2 throttling for ip " + client.identity);
return(synth(429, "ETOOMANYREQUESTS"));
}
}
if (vsthrottle.is_denied("ip:" + client.identity, 500, 10s)) {
std.log("global throttling for ip " + client.identity);
return(synth(429, "ETOOMANYREQUESTS"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment