Skip to content

Instantly share code, notes, and snippets.

I may be slow to respond.

Huang Huang mozillazg

I may be slow to respond.
View GitHub Profile

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.

mozillazg /
Created Dec 5, 2017
A simple demo for how to use flask-paginate.
from flask import Flask, render_template
from flask_paginate import Pagination, get_page_args
app = Flask(__name__)
app.template_folder = ''
users = list(range(100))
def get_users(offset=0, per_page=10):
mozillazg / Mac SSH Autocomplete
Last active Oct 13, 2017 — forked from aliang/Mac SSH Autocomplete
Add auto complete to your ssh, put into your .bash_profile
View Mac SSH Autocomplete
_complete_ssh_hosts ()
comp_ssh_hosts=`cat ~/.ssh/known_hosts | \
cut -f 1 -d ' ' | \
sed -e s/,.*//g | \
grep -v ^# | \
uniq | \
grep -v "\[" ;
mozillazg / pre-commit
Created May 1, 2016
protect master branch
View pre-commit
protect_branch() {
current_name=$(git symbolic-ref HEAD | sed -e "s/^refs\/heads\///")
if [ "$current_name" = "$protected_name" ]; then
echo "!!! OH NO! you are trying to change the **$protected_name** branch!!!"
exit 1
protect_branch master
mozillazg / go_coverage
Last active May 1, 2016
Generate test coverage statistics for Go packages.
View go_coverage
# Generate test coverage statistics for Go packages.
# Works around the fact that `go test -coverprofile` currently does not work
# with multiple packages, see
# Usage: go_coverage [ [--html | html] | --coveralls | --help ]
# --html Additionally create HTML report and open it in browser
# --coveralls Push coverage statistics to
mozillazg / nginx-nodejs-cors
Created Jan 15, 2016 — forked from m4ttbrock/nginx-nodejs-cors
Nginx Nodejs CORS to subdomain
View nginx-nodejs-cors
server {
listen 80;
access_log /var/log/nginx/example.access.log;
location / {
if ($http_origin ~* "https?://.*\.example\.com(:[0-9]+)?") {
set $cors "true";
mozillazg / supervisord.service
Last active May 12, 2022 — forked from tonyseek/supervisord.service
install and configure supervisord on centos 7.
View supervisord.service
Description=supervisord - Supervisor process control system for UNIX
ExecStart=/bin/supervisord -c /etc/supervisord/supervisord.conf
ExecReload=/bin/supervisorctl reload
ExecStop=/bin/supervisorctl shutdown
mozillazg / vim74_lua
Created Nov 20, 2015 — forked from jdewit/vim74_lua
Installing vim 7.4 with lua on Ubuntu 12.04
View vim74_lua
sudo apt-get remove --purge vim vim-runtime vim-gnome vim-tiny vim-common vim-gui-common
sudo apt-get build-dep vim-gnome
sudo apt-get install liblua5.1-dev luajit libluajit-5.1 python-dev ruby-dev libperl-dev libncurses5-dev libgnome2-dev libgnomeui-dev libgtk2.0-dev libatk1.0-dev libbonoboui2-dev libcairo2-dev libx11-dev libxpm-dev libxt-dev
sudo rm -rf /usr/local/share/vim
sudo rm /usr/bin/vim
mozillazg /
Created Nov 16, 2015 — forked from sloria/
A "Best of the Best Practices" (BOBP) guide to developing in Python.

The Best of the Best Practices (BOBP) Guide for Python

A "Best of the Best Practices" (BOBP) guide to developing in Python.

In General


  • "Build tools for others that you want to be built for you." - Kenneth Reitz
  • "Simplicity is alway better than functionality." - Pieter Hintjens
mozillazg / upload_file.go
Created Dec 17, 2014
Upload file by go. Support custom headers.
View upload_file.go
package main
import (