Skip to content

Instantly share code, notes, and snippets.

alert("hi");
# see https://www.topbug.net/blog/2013/04/14/install-and-use-gnu-command-line-tools-in-mac-os-x/
# core
brew install coreutils
# key commands
brew install binutils
brew install diffutils
brew install ed --default-names
brew install findutils --with-default-names

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.

@samqiu
samqiu / latency.markdown
Created November 29, 2018 12:01 — forked from hellerbarde/latency.markdown
Latency numbers every programmer should know

Latency numbers every programmer should know

L1 cache reference ......................... 0.5 ns
Branch mispredict ............................ 5 ns
L2 cache reference ........................... 7 ns
Mutex lock/unlock ........................... 25 ns
Main memory reference ...................... 100 ns             
Compress 1K bytes with Zippy ............. 3,000 ns  =   3 µs
Send 2K bytes over 1 Gbps network ....... 20,000 ns  =  20 µs
SSD random read ........................ 150,000 ns  = 150 µs

Read 1 MB sequentially from memory ..... 250,000 ns = 250 µs

@samqiu
samqiu / demo.tex
Created September 7, 2017 12:17 — forked from Liam0205/demo.tex
demo of moderncv
%!TEX program = xelatex
% Font Size:
% 10pt, 11pt, 12pt
% Paper Size:
% a4paper, letterpaper, a5paper, leagalpaper, executivepaper, landscape
% Font Family:
% roman, sans
\documentclass[12pt, a4paper, roman]{moderncv}
% Style:
@samqiu
samqiu / ip_in_range.php
Created August 22, 2017 02:32 — forked from tott/ip_in_range.php
php check if IP is in given network range
/**
* Check if a given ip is in a network
* @param string $ip IP to check in IPV4 format eg. 127.0.0.1
* @param string $range IP/CIDR netmask eg. 127.0.0.0/24, also 127.0.0.1 is accepted and /32 assumed
* @return boolean true if the ip is in this range / false if not.
*/
function ip_in_range( $ip, $range ) {
if ( strpos( $range, '/' ) == false ) {
$range .= '/32';
}
@samqiu
samqiu / psql-with-gzip-cheatsheet.sh
Created August 13, 2017 13:48 — forked from brock/psql-with-gzip-cheatsheet.sh
Exporting and Importing Postgres Databases using gzip
# This is just a cheat sheet:
# On production
sudo -u postgres pg_dump database | gzip -9 > database.sql.gz
# On local
scp -C production:~/database.sql.gz
dropdb database && createdb database
gunzip < database.sql.gz | psql database
@samqiu
samqiu / _service.md
Created January 7, 2017 11:15 — forked from naholyr/_service.md
Sample /etc/init.d script

Sample service script for debianoids

Look at LSB init scripts for more information.

Usage

Copy to /etc/init.d:

# replace "$YOUR_SERVICE_NAME" with your service's name (whenever it's not enough obvious)

电视里看到一个专题片,因为最后事发时只有熟女在场,片子里大部分是这个熟女在讲述。

说是一对中年夫妻,有一对十几岁的女儿,家庭幸福。可后来有一天老公忽然向熟女坦白他有外遇,熟女的回答是:你赶快改正错误!

老公对这个反应很惊讶,因为一般女人听到这种事都会跳起来,但他仍然不愿意改。熟女在一家律师事务所做办公助理,忽然被解雇,雪上加霜。熟女人生陷入低谷。

正好此时有个年轻小伙法学院毕业,刚开了个自己的事务所,熟女去申请工作,立刻就被录用,然后俩人就搞在了一起。熟女说:老公有了外遇后,我只是想被人爱,被人捧,其他什么都没想。

他们的关系后来被她老公察觉,老公很愤怒,但熟女想办法骗了过去。

@samqiu
samqiu / RubyConfChina.geojson
Last active December 21, 2015 22:38
RubyConfChina 2013 开始接受报名和售票!
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.