Skip to content

Instantly share code, notes, and snippets.

@hsiboy
Last active August 29, 2015 14:01
Show Gist options
  • Save hsiboy/713b4099812050b5a7ff to your computer and use it in GitHub Desktop.
Save hsiboy/713b4099812050b5a7ff to your computer and use it in GitHub Desktop.
Zeus Traffic Manager - Bandwidth Management and Rate Shaping (based on referer)
# Automatically limit all requests from any referer.
# Uses two rate classes, BusyReferer for those sites that send a large amount of traffic
# and StandardReferers for those that don't.
# Referer whitelist. These referers are never rate limited.
$whitelist = "localhost 172.16.121.100";
# Referers that are allowed to pass a higher number of clients.
$highTraffic = "google mypartner.com";
# How many queued requests are allowed before we track users.
$shapeQueue = 2;
# Retrieve the referer and strip out the domain name part.
$referer = http.getheader("Referer");
$referer = String.regexsub($referer, ".*?://(.*?)/.*", "$1", "i" );
# Check to see if this user has already been given an abuse cookie.
# If they have we'll force them into a bandwidth class
if ( $cookie = http.getCookie("AbusiveReferer") )
{
response.setBandwidthClass("AbusiveReferer");
}
# If the referer is whitelisted then exit.
if ( String.contains( $whitelist, $referer ) )
{
break;
}
# Put the incoming users through the busy or standard rate classes
# and check the queue length for their referer.
if ( String.contains( $highTraffic, $referer ) )
{
$backlog = rate.getbacklog("BusyReferer", $referer);
rate.use("BusyReferer", $referer);
} else {
$backlog = rate.getbacklog("StandardReferer", $referer);
rate.use("StandardReferer", $referer);
}
# If we have exceeded our backlog limit, then give them a cookie
# this will enforce bandwidth shaping for subsequent requests.
if ( $backlog > $shapeQueue )
{
http.setResponseCookie("AbusiveReferer", $referer);
response.setBandwidthClass("AbusiveReferer");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment