Skip to content

Instantly share code, notes, and snippets.

View mozillazg's full-sized avatar
💭
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
mozillazg / app.py
Created December 5, 2017 00:06
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
mozillazg / Mac SSH Autocomplete
Last active October 13, 2017 07:16 — forked from aliang/Mac SSH Autocomplete
Add auto complete to your ssh, put into your .bash_profile
_complete_ssh_hosts ()
{
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
comp_ssh_hosts=`cat ~/.ssh/known_hosts | \
cut -f 1 -d ' ' | \
sed -e s/,.*//g | \
grep -v ^# | \
uniq | \
grep -v "\[" ;
@mozillazg
mozillazg / pre-commit
Created May 1, 2016 04:14
protect master branch
protect_branch() {
protected_name=$1
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
fi
}
protect_branch master
@mozillazg
mozillazg / go_coverage
Last active May 1, 2016 03:57
Generate test coverage statistics for Go packages. https://mlafeldt.github.io/blog/test-coverage-in-go/
!/bin/sh
# Generate test coverage statistics for Go packages.
#
# Works around the fact that `go test -coverprofile` currently does not work
# with multiple packages, see https://code.google.com/p/go/issues/detail?id=6909
#
# Usage: go_coverage [ [--html | html] | --coveralls | --help ]
#
# --html Additionally create HTML report and open it in browser
# --coveralls Push coverage statistics to coveralls.io
@mozillazg
mozillazg / nginx-nodejs-cors
Created January 15, 2016 11:20 — forked from m4ttbrock/nginx-nodejs-cors
Nginx Nodejs CORS to subdomain
server {
listen 80;
server_name subdomain.example.com;
access_log /var/log/nginx/example.access.log;
location / {
if ($http_origin ~* "https?://.*\.example\.com(:[0-9]+)?") {
set $cors "true";
@mozillazg
mozillazg / supervisord.service
Last active December 1, 2023 12:59 — forked from tonyseek/supervisord.service
install and configure supervisord on centos 7.
[Unit]
Description=supervisord - Supervisor process control system for UNIX
Documentation=http://supervisord.org
After=network.target
[Service]
Type=forking
ExecStart=/bin/supervisord -c /etc/supervisord/supervisord.conf
ExecReload=/bin/supervisorctl reload
ExecStop=/bin/supervisorctl shutdown
@mozillazg
mozillazg / vim74_lua
Created November 20, 2015 07:27 — forked from jdewit/vim74_lua
Installing vim 7.4 with lua on Ubuntu 12.04
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
mozillazg / bobp-python.md
Created November 16, 2015 06:14 — forked from sloria/bobp-python.md
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

Values

  • "Build tools for others that you want to be built for you." - Kenneth Reitz
  • "Simplicity is alway better than functionality." - Pieter Hintjens
@mozillazg
mozillazg / upload_file.go
Created December 17, 2014 05:33
Upload file by go. Support custom headers.
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"os"