Skip to content

Instantly share code, notes, and snippets.

View lancetw's full-sized avatar
🐈

Hsin-lin Cheng lancetw

🐈
View GitHub Profile
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from pygments import highlight
from pygments.lexers import JsonLexer
from pygments.formatters import TerminalFormatter
from pprint import pformat
import json
def pprint_color(obj, indent=2):
export const submit = (values, dispatch /*, props */) => {
setSubmitting('loginForm', true)
return login(values)
.then(action => {
dispatch(action)
setSubmitting('loginForm', false)
})
.catch(error => {
dispatch(error)
throw new SubmissionError(error.payload)
let actions = []
for (let i = 0; i < 10000; i++) {
actions.push(() => Promise.resolve(i))
}
const process = (task) => task.reduce((promised, task) => promised.then(acc => task().then(value => [...acc, value])), Promise.resolve([]))
process(actions)
const actions = [
() => Promise.resolve(0),
() => Promise.resolve(1),
() => Promise.resolve(2),
() => Promise.resolve(3),
() => Promise.reject(new Error('4')),
() => Promise.resolve(5),
() => Promise.reject(new Error('6')),
() => Promise.reject(new Error('7'))
]
#include <iostream>
#include <regex>
using namespace std;
auto split(const string &s, const string &gex = "\\s+") {
regex re(gex);
vector<string> ret{sregex_token_iterator(s.begin(), s.end(), re, -1), {}};
if (0 == ret.begin()->compare("")) ret.clear();
return ret;
}

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.

char** fizzBuzz(int n, int* returnSize) {
char** ret = calloc(n, sizeof(char*));
*returnSize = n;
char* sp;
for (int i = 1; i <= n; ++i) {
sp = *(ret + (i - 1)) = calloc(1, sizeof(char));
(i % 5 && i % 3) ? sprintf(sp, "%d", i) : sprintf(sp, "%s%s", (i % 3) ? "" : "Fizz", (i % 5) ? "" : "Buzz");
}
var fact = (function (fact) {
return (function (f) {
return f(function (f) {
return fact(function (n) {
return f(f)(n)
})
})
})(function (f) {
return f(f)
})
@lancetw
lancetw / hch.py
Last active December 4, 2016 13:42 — forked from lanfon72/hch.py
NTU hospital hsin-chu branch ER board.
# !/usr/bin/env python
# coding:UTF-8
import requests, json, re
from datetime import datetime
html = requests.get('http://reg.ntuh.gov.tw/EmgInfoBoard/NTUHEmgInfoT4.aspx', verify=False)
keys = ['pending_doctor', 'pending_ward', 'pending_icu', 'pending_bed']
pending0 = re.findall(r"<td(.*?)>(.+?)</td>", html.text)
@lancetw
lancetw / hmmh.py
Last active December 19, 2018 15:27
Mackay memorial hospital Hsinchu branch ER board. (updated 20181219)
# !/usr/bin/env python
# coding:UTF-8
import requests, json, re
from datetime import datetime
requests.packages.urllib3.disable_warnings()
html = requests.get('https://wapps.mmh.org.tw/WebEMR/WebEMR/Default.aspx?a=HC', verify=False)
keys = ['pending_doctor', 'pending_bed', 'pending_ward', 'pending_icu']