Skip to content

Instantly share code, notes, and snippets.

@risent
risent / latency.txt
Created April 1, 2021 06:45 — forked from jboner/latency.txt
Latency Numbers Every Programmer Should Know
Latency Comparison Numbers (~2012)
----------------------------------
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns 14x L1 cache
Mutex lock/unlock 25 ns
Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 3,000 ns 3 us
Send 1K bytes over 1 Gbps network 10,000 ns 10 us
Read 4K randomly from SSD* 150,000 ns 150 us ~1GB/sec SSD
@risent
risent / parse-addr.js
Last active November 7, 2019 09:03
从地址中解析出对应的行政区划
const parseAddr = (addr) => {
const sepList = ['省', '市', '市', '县', '区', '街道', '乡', '镇']
const res = []
let rest = addr
for (let sep of sepList) {
let m = rest.split(sep, 2)
let current
if (m.length > 1) {
current = m[0] + sep
@risent
risent / tweet_bookmarklet.js
Last active May 30, 2018 02:45
Twitter Shared Button Bookmarlet
fcuntion() {
var text = encodeURIComponent(window.getSelection().toString() || window.document.title);
var url = encodeURIComponent(window.location.href);
var dstUrl = 'https://twitter.com/intent/tweet?url=' + url + '&text=' + text;
var windowName = 'Tweet Intent';
var features = 'width=600,height=350,left=600,top=200';
window.open(dstUrl, windowName, features);
};
@risent
risent / postgresql_id.sql
Created May 10, 2018 09:43 — forked from yohangdev/postgresql_id.sql
PostgreSQL Better ID & UUID Generator
create schema shard_1;
create sequence shard_1.global_id_sequence;
CREATE OR REPLACE FUNCTION shard_1.id_generator(OUT result bigint) AS $$
DECLARE
our_epoch bigint := 1314220021721;
seq_id bigint;
now_millis bigint;
-- the id of this DB shard, must be set for each
-- schema shard you have - you could pass this as a parameter too

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.

@risent
risent / rocksdb.lua
Created March 24, 2018 06:27
Lua binding for RocksDB
local ffi = require("ffi")
-- cut from https://github.com/facebook/rocksdb/blob/e59cb2a19b2971707d75552ae517586ca0785a6a/include/rocksdb/c.h
ffi.cdef[[
/* Exported types */
typedef struct rocksdb_t rocksdb_t;
typedef struct rocksdb_backup_engine_t rocksdb_backup_engine_t;
typedef struct rocksdb_backup_engine_info_t rocksdb_backup_engine_info_t;
typedef struct rocksdb_restore_options_t rocksdb_restore_options_t;
@risent
risent / leveldb.py
Last active March 24, 2018 06:22
Python LevelDB Simple Code use CFFI
from cffi import FFI
ffi = FFI()
# copied from https://github.com/google/leveldb/blob/master/include/leveldb/c.h
ffi.cdef("""
/* Exported types */
typedef struct leveldb_t leveldb_t;

Hello World In Python 10 Ways

1. general

print('Hello World')

2. sys stdout

import sys
// demo_cpu.c
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#define ISROM(a) ((a) < 16)
#define ISRAM(a) ((a) >= 16 && (a) < 20)
@risent
risent / # gcc - 2018-03-07_09-59-22.txt
Created March 7, 2018 02:03
gcc on macOS 10.13.3 - Homebrew build logs
Homebrew build logs for gcc on macOS 10.13.3
Build date: 2018-03-07 09:59:22