Skip to content

Instantly share code, notes, and snippets.

Scaling your API with rate limiters

The following are examples of the four types rate limiters discussed in the accompanying blog post. In the examples below I've used pseudocode-like Ruby, so if you're unfamiliar with Ruby you should be able to easily translate this approach to other languages. Complete examples in Ruby are also provided later in this gist.

In most cases you'll want all these examples to be classes, but I've used simple functions here to keep the code samples brief.

Request rate limiter

This uses a basic token bucket algorithm and relies on the fact that Redis scripts execute atomically. No other operations can run between fetching the count and writing the new count.

Verifying I am +ptarjan on my passcard. https://onename.com/ptarjan
[ 0%] Built target fastlz
[ 0%] Built target afdt
[ 5%] Built target mbfl
[ 6%] Built target sqlite3
[ 6%] Built target timelib
[ 6%] Built target lz4
[ 6%] Built target double-conversion
[ 9%] Built target folly
[ 15%] Built target zip_static
[ 16%] Built target hphp_thrift
commit e4047dc130d0fe05efb989f448f5ebe1b7a3bd31
Author: Paul Tarjan <ptarjan@fb.com>
Date: 10 days ago
support display_errors
diff --git a/hphp/runtime/base/execution-context.cpp b/hphp/runtime/base/execution-context.cpp
index 2d83a46..07ae352 100644
--- a/hphp/runtime/base/execution-context.cpp
+++ b/hphp/runtime/base/execution-context.cpp
diff --git a/hphp/runtime/server/fastcgi/fastcgi-transport.cpp b/hphp/runtime/server/fastcgi/fastcgi-transport.cpp
index 6372f1e..fbe71d6 100644
--- a/hphp/runtime/server/fastcgi/fastcgi-transport.cpp
+++ b/hphp/runtime/server/fastcgi/fastcgi-transport.cpp
@@ -449,7 +449,7 @@ void FastCGITransport::onHeadersComplete() {
m_scriptFilename = getRawHeader(s_pathTranslated);
}
- // do a check for mod_proxy_cgi and remove the start portion of the string
+ // do a check for mod_proxy_fcgi and remove the start portion of the string
ptarjan@ubuntu:/tmp/fcgiquery$ ./vendor/bin/heroku-hhvm-apache2
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
Booting on port 16762...
Using HHVM configuration (php.ini) file '/heroku/heroku-buildpack-php/conf/hhvm/php.ini'
Using Apache2 VirtualHost-level configuration include '/heroku/heroku-buildpack-php/conf/apache2/default_include.conf'
Using Apache2 configuration file '/heroku/heroku-buildpack-php/conf/apache2/heroku.conf'
Starting log redirection...
Starting hhvm...
Starting httpd...
mapping self...
ptarjan@ubuntu:/tmp/fcgiquery/vendor$ ./bin/heroku-hhvm-apache2
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
Booting on port 4221...
Using HHVM configuration (php.ini) file '/heroku/heroku-buildpack-php/conf/hhvm/php.ini'
Using Apache2 VirtualHost-level configuration include '/heroku/heroku-buildpack-php/conf/apache2/default_include.conf'
Using Apache2 configuration file '/heroku/heroku-buildpack-php/conf/apache2/heroku.conf'
Starting log redirection...
Starting hhvm...
Starting httpd...
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
ptarjan@ubuntu:/tmp/fcgiquery$ httpd -t -D DUMP_MODULES
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
Loaded Modules:
core_module (static)
so_module (static)
http_module (static)
mpm_event_module (static)
authn_file_module (shared)
authn_core_module (shared)
authz_host_module (shared)
ptarjan@ubuntu:/tmp/fcgiquery$ sudo /etc/init.d/apache2 stop; sudo vendor/bin/heroku-hhvm-apache2
* Stopping web server apache2 *
Booting on port 4631...
Using HHVM configuration (php.ini) file 'vendor/heroku/heroku-buildpack-php/conf/hhvm/php.ini'
Using Apache2 VirtualHost-level configuration include 'vendor/heroku/heroku-buildpack-php/conf/apache2/default_include.conf'
Using Apache2 configuration file 'vendor/heroku/heroku-buildpack-php/conf/apache2/heroku.conf'
Starting log redirection...
Starting hhvm...
Starting httpd...
Process exited unexpectedly: * Starting web server apache2
ptarjan@ubuntu:/tmp/fcgiquery$ export | grep APA
declare -x APACHE_LOCK_DIR="/var/lock/apache2"
declare -x APACHE_LOG_DIR="/var/log/apache2"
declare -x APACHE_PID_FILE="/var/run/apache2/apache2.pid"
declare -x APACHE_RUN_DIR="/var/run/apache2"
declare -x APACHE_RUN_GROUP="www-data"
declare -x APACHE_RUN_USER="www-data"