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.

@ptarjan
ptarjan / gpg-encrypt.sh
Created May 25, 2009 00:54
gpg-encrypt . A simple script for keeping a file gpg encrypted.
#!/bin/sh
filename="passwords.gpg"
if [ $EDITOR ]
then
editor=$EDITOR
elif [ -f /usr/bin/editor ]
then
editor=/usr/bin/editor
else
editor=vi
diff --git i/hphp/runtime/server/http-protocol.cpp w/hphp/runtime/server/http-protocol.cpp
index 205ab06..cc0342d 100644
--- i/hphp/runtime/server/http-protocol.cpp
+++ w/hphp/runtime/server/http-protocol.cpp
@@ -558,7 +558,10 @@ void HttpProtocol::CopyPathInfo(Variant& server,
server.set(s_SCRIPT_NAME, r.resolvedURL());
}
- if (r.rewritten()) {
+ auto scriptFilename = transport->getPathTranslated();
diff --git a/hphp/runtime/server/fastcgi/fastcgi-transport.cpp b/hphp/runtime/server/fastcgi/fastcgi-transport.cpp
index 0dcff6f..4c1e392 100644
--- a/hphp/runtime/server/fastcgi/fastcgi-transport.cpp
+++ b/hphp/runtime/server/fastcgi/fastcgi-transport.cpp
@@ -56,8 +56,8 @@ const char *FastCGITransport::getUrl() {
return m_requestURI.c_str();
}
-const std::string FastCGITransport::getScriptFilename() {
- return m_scriptFilename;
diff --git a/hphp/runtime/server/fastcgi/fastcgi-transport.cpp b/hphp/runtime/server/fastcgi/fastcgi-transport.cpp
index 0dcff6f..0a01bc0 100644
--- a/hphp/runtime/server/fastcgi/fastcgi-transport.cpp
+++ b/hphp/runtime/server/fastcgi/fastcgi-transport.cpp
@@ -56,8 +56,8 @@ const char *FastCGITransport::getUrl() {
return m_requestURI.c_str();
}
-const std::string FastCGITransport::getScriptFilename() {
- return m_scriptFilename;
diff --git a/hphp/runtime/server/fastcgi/fastcgi-transport.cpp b/hphp/runtime/server/fastcgi/fastcgi-transport.cpp
index d7f7501..39b1f84 100644
--- a/hphp/runtime/server/fastcgi/fastcgi-transport.cpp
+++ b/hphp/runtime/server/fastcgi/fastcgi-transport.cpp
@@ -315,12 +315,6 @@ void FastCGITransport::onHeader(std::unique_ptr<folly::IOBuf> key_chain,
std::unique_ptr<folly::IOBuf> value_chain) {
Cursor cursor(key_chain.get());
std::string key = cursor.readFixedString(key_chain->computeChainDataLength());
-
- // HTTP has case insensitive header keys
-------------------------- after initial translation ---------------------------
B0:
Method public (leaf) A::c at 27
--- bc 27, spOff 0 (A::c)
27: String "b.php"
(00) t0:FramePtr = DefFP
(01) t1:StkPtr = DefSP<0> t0:FramePtr
--- bc 32, spOff 1 (A::c)
diff --git a/hphp/runtime/server/fastcgi/fastcgi-transport.cpp b/hphp/runtime/server/fastcgi/fastcgi-transport.cpp
index ae0dddf..28e719a 100644
--- a/hphp/runtime/server/fastcgi/fastcgi-transport.cpp
+++ b/hphp/runtime/server/fastcgi/fastcgi-transport.cpp
@@ -303,6 +303,7 @@ void FastCGITransport::onBodyComplete() {
const std::string FastCGITransport::k_requestURIKey = "REQUEST_URI";
const std::string FastCGITransport::k_remoteHostKey = "REMOTE_HOST";
+const std::string FastCGITransport::k_remoteAddrKey = "REMOTE_ADDR";
const std::string FastCGITransport::k_remotePortKey = "REMOTE_PORT";
diff --git a/hphp/runtime/server/fastcgi/fastcgi-server.cpp b/hphp/runtime/server/fastcgi/fastcgi-server.cpp
index 93bee22..7bd2de4 100644
--- a/hphp/runtime/server/fastcgi/fastcgi-server.cpp
+++ b/hphp/runtime/server/fastcgi/fastcgi-server.cpp
@@ -118,12 +118,10 @@ void FastCGIConnection::readDataAvailable(size_t len) noexcept {
void FastCGIConnection::readEOF() noexcept {
shutdownTransport();
- delete this;
}
@ptarjan
ptarjan / gist:7649248
Created November 25, 2013 21:31
HHVM Project Theme for Open Academy - 2013/14

HHVM

HHVM is a an open-source virtual machine designed for executing programs written in PHP. HHVM uses a just-in-time compilation approach to achieve superior performance while maintaining the flexibility and ease of use that PHP developers are accustomed to (dynamic features like eval(), rapid run-edit-debug cycle, etc).

HHVM is used by Facebook to serve billions of web requests per day. To date, HHVM has realized over a 9x increase in web request throughput and over a 5x reduction in memory consumption for Facebook compared with the Zend PHP 5.2 engine + APC.

You can learn more going through our wiki.

Objectives