Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Focusing

dushujun

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

View 百家姓.txt
View keybase.md

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 Oct 23, 2017 — forked from paulirish/readme.md
resolving the proper location and line number through a console.log wrapper
View readme.md

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.

View playground.rs
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 Jul 20, 2017 — forked from staltz/introrx.md
The introduction to Reactive Programming you've been missing
View introrx.md
@dushujun
dushujun / es_query_by_string_length.json
Created May 4, 2017
elasticsearch query by string length
View es_query_by_string_length.json
{
"query": {
"bool": {
"must": {
"script": {
"script": {
"inline": "doc['被执行人姓名/名称.raw'].getValue().length() < 4 ",
"lang": "painless"
}
}
View str2unicode.py
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 Sep 26, 2016 — forked from addyosmani/README.md
108 byte CSS Layout Debugger
View README.md

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 Aug 24, 2016 — forked from gordonbrander/ID.js
ID - a unique ID/name generator for JavaScript
View ID.js
// 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`
You can’t perform that action at this time.