Skip to content

Instantly share code, notes, and snippets.

💭
I may be slow to respond.

Huang Huang mozillazg

💭
I may be slow to respond.
Block or report user

Report or block mozillazg

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View 0-rate-limiters.md

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 Dec 5, 2017
A simple demo for how to use flask-paginate.
View app.py
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 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 ()
{
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
protect master branch
View pre-commit
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
Generate test coverage statistics for Go packages. https://mlafeldt.github.io/blog/test-coverage-in-go/
View go_coverage
!/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 Jan 15, 2016 — forked from m4ttbrock/nginx-nodejs-cors
Nginx Nodejs CORS to subdomain
View nginx-nodejs-cors
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 Feb 19, 2020 — forked from tonyseek/supervisord.service
install and configure supervisord on centos 7.
View supervisord.service
[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 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
mozillazg / bobp-python.md
Created Nov 16, 2015 — forked from sloria/bobp-python.md
A "Best of the Best Practices" (BOBP) guide to developing in Python.
View bobp-python.md

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 Dec 17, 2014
Upload file by go. Support custom headers.
View upload_file.go
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"os"
You can’t perform that action at this time.