from flask import Flask, send_from_directory
app = Flask(__name__)
def base_static(filename):
return send_from_directory(app.root_path + '/../static/', filename)

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.


Make it real

Ideas are cheap. Make a prototype, sketch a CLI session, draw a wireframe. Discuss around concrete examples, not hand-waving abstractions. Don't say you did something, provide a URL that proves it.

Ship it

Nothing is real until it's being used by a real user. This doesn't mean you make a prototype in the morning and blog about it in the evening. It means you find one person you believe your product will help and try to get them to use it.

Do it with style

View Global keyboard hook for OSX
// alterkeys.c
// Complile using the following command line:
// gcc -Wall -o alterkeys alterkeys.c -framework ApplicationServices
// You need superuser privileges to create the event tap, unless accessibility
// is enabled. To do so, select the "Enable access for assistive devices"
// checkbox in the Universal Access system preference pane.
# By Steve Hanov, 2011. Released to the public domain.
# Updated 2014 to use DAWG as a mapping.
import sys
import time
DICTIONARY = "/usr/share/dict/words"
QUERY = sys.argv[1:]
# This class represents a node in the directed acyclic word graph (DAWG). It
View get_title_and_url.applescript
# Keep in mind that when asking for a `return` after another, only the first one will be output.
# This example is meant as a simple starting point, to show how to get the information in the simplest available way.
# Google Chrome
tell application "Google Chrome" to return URL of active tab of front window
tell application "Google Chrome" to return title of active tab of front window
# Google Chrome Canary
tell application "Google Chrome Canary" to return URL of active tab of front window
tell application "Google Chrome Canary" to return title of active tab of front window
# Chromium
View expng.ex
defmodule Expng do
defstruct [:width, :height, :bit_depth, :color_type, :compression, :filter, :interlace, :chunks]
def png_parse(<<
0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A,
_length :: size(32),
width :: size(32),
height :: size(32),
View css-responsive-images.html
<!DOCTYPE html>
<meta charset="utf-8">
<title>CSS responsive images</title>
/* Doesn't stop original source image being
downloaded too */
@media (min-device-width:600px) {
View uri.js
var parser = document.createElement('a');
parser.href = "";
parser.protocol; // => "http:"
parser.hostname; // => ""
parser.port; // => "3000"
parser.pathname; // => "/pathname/"; // => "?search=test"
parser.hash; // => "#hash"; // => ""