Skip to content

Instantly share code, notes, and snippets.

View dushujun's full-sized avatar
🎯
Focusing

dushujun

🎯
Focusing
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.

@dushujun
dushujun / 百家姓.txt
Created November 6, 2017 03:04
百家姓

Keybase proof

I hereby claim:

  • I am dushujun on github.
  • I am shujun (https://keybase.io/shujun) on keybase.
  • I have a public key ASDvp0WQVChQYr6fDay77M7gFMuHvHpKkDegXLkuKwc58go

To claim this, I am signing this object:

@dushujun
dushujun / readme.md
Created October 23, 2017 06:32 — forked from paulirish/readme.md
resolving the proper location and line number through a console.log wrapper

console.log wrap resolving for your wrapped console logs

I've heard this before:

What I really get frustrated by is that I cannot wrap console.* and preserve line numbers

We enabled this in Chrome DevTools via blackboxing a bit ago.

If you blackbox the script file the contains the console log wrapper, the script location shown in the console will be corrected to the original source file and line number. Click, and the full source is looking longingly into your eyes.

@dushujun
dushujun / playground.rs
Last active September 4, 2017 09:28 — forked from anonymous/playground.rs
rust macro
macro_rules! enum_with_str_representation {
(enum $enum_name:ident {
$($variant:ident => $nice_name:expr,)+
}) => {
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
enum $enum_name {
$($variant),+
}
impl ::std::fmt::Display for $enum_name {
@dushujun
dushujun / introrx.md
Created July 20, 2017 08:32 — forked from staltz/introrx.md
The introduction to Reactive Programming you've been missing
@dushujun
dushujun / es_query_by_string_length.json
Created May 4, 2017 02:57
elasticsearch query by string length
{
"query": {
"bool": {
"must": {
"script": {
"script": {
"inline": "doc['被执行人姓名/名称.raw'].getValue().length() < 4 ",
"lang": "painless"
}
}
def str2unicode(data):
"""从bt拿到的任务参数是str, 需要转换成unicode"""
if isinstance(data, str):
return data.decode('utf8')
elif isinstance(data, unicode):
return data
elif isinstance(data, collections.Mapping):
return dict(map(str2unicode, data.iteritems()))
elif isinstance(data, collections.Iterable):
return type(data)(map(str2unicode, data))
@dushujun
dushujun / README.md
Created September 26, 2016 23:36 — forked from addyosmani/README.md
108 byte CSS Layout Debugger

CSS Layout Debugger

A tweet-sized debugger for visualizing your CSS layouts. Outlines every DOM element on your page a random (valid) CSS hex color.

One-line version to paste in your DevTools

Use $$ if your browser aliases it:

~ 108 byte version

@dushujun
dushujun / ID.js
Created August 24, 2016 08:30
ID - a unique ID/name generator for JavaScript
// Generate unique IDs for use as pseudo-private/protected names.
// Similar in concept to
// <http://wiki.ecmascript.org/doku.php?id=strawman:names>.
//
// The goals of this function are twofold:
//
// * Provide a way to generate a string guaranteed to be unique when compared
// to other strings generated by this function.
// * Make the string complex enough that it is highly unlikely to be
// accidentally duplicated by hand (this is key if you're using `ID`