Skip to content

Instantly share code, notes, and snippets.

View yoshuawuyts's full-sized avatar

Yosh yoshuawuyts

View GitHub Profile
#!/usr/bin/env node
var path = require('path')
var parse = require('minimist')
var browserify = require('browserify')
var args = parse(process.argv.slice(2))
var entries = args._
var output = args.o || args.output
var main = args.m || args.main
var url = args.url || output
@yoshuawuyts
yoshuawuyts / npm-classic-config.sh
Last active July 5, 2017 12:36 — forked from mafintosh/npm-classic-config.sh
Resets the npm defaults to how they used to be
npm config set loglevel http
npm config set progress false
npm config set shrinkwrap false
npm config set save false
npm config set package-lock false
mkdir -p ~/.config/configstore/
printf '{"optOut": true,"lastUpdateCheck": 0}' > ~/.config/configstore/update-notifier-npm.json

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.

@yoshuawuyts
yoshuawuyts / build-gn.sh
Created October 13, 2016 19:34 — forked from mohamed/build-gn.sh
Build Google gn build tool standalone
#!/bin/bash
set -e
set -v
# Get the sources
mkdir gn-standalone
cd gn-standalone
mkdir tools
cd tools
@yoshuawuyts
yoshuawuyts / memoizeNode.js
Last active October 7, 2016 15:14 — forked from kristoferjoseph/memoizeNode.js
Helper function used in memoization of an HTML Node.
const elType = 'div'
module.exports = function memoizeNode (node) {
var placeholder = null
var element = null
var args = null
return function render () {
const _args = arguments
if (!element) {
@yoshuawuyts
yoshuawuyts / fast.md
Last active August 27, 2016 16:43 — forked from davidmarkclements/fast.md
Idea - Single Request SPA
@yoshuawuyts
yoshuawuyts / firstJank.js
Created August 16, 2016 17:12 — forked from junosuarez/firstJank.js
key RUM perf metric: time-to-first-jank
if (window.requestAnimationFrame) {
function checkForJank () { // eslint-disable-line no-inner-delcarations
const frameStart = Date.now()
window.requestAnimationFrame(() => {
const frameDuration = Date.now() - frameStart
if (frameDuration > 17) {
Perf.log(FirstJank)
} else {
checkForJank()
}
alias ..='cd ..'
alias ls='gls --color=auto'
alias ll='ls -lh --group-directories-first'
alias slt='open -a "Sublime Text"'
alias npm-offline="npm --cache-min Infinity "
alias publish='git push && git push --tags && npm publish'
alias nodeir='node --trace-hydrogen --trace-phase=Z --trace-deopt --code-comments --hydrogen-track-positions --redirect-code-traces --redirect-code-traces-to=code.asm --print-opt-code --trace_hydrogen_file=hydrogen.cfg'
export HISTCONTROL=ignoredups
export PS1="\[\033[44m\] \W \[\033[49m\]\[\033[42m\]\$(__git_ps1 \" %s \")\[\033[49m\] "
@yoshuawuyts
yoshuawuyts / choo.txt
Created July 21, 2016 01:24 — forked from ahdinosaur/choo.txt
chat logs from freenode #choo
20:04 < ahdinosaur> if anyone is interested in a choo-like API where it's pull-streams all the way down, i made some helpers for `inu`: https://github.com/ahdinosaur/inux
20:04 < yoshuawuyts> ahdinosaur: I'm curious: how big is inu?
20:04 < yoshuawuyts> probably smaller than choo I'd think
20:05 < yoshuawuyts> well, inux would be more fair to compare I reckon but still
20:05 < ahdinosaur> one sec, i'll discify
20:05 < yoshuawuyts> ahdinosaur: I'm using this for choo https://github.com/yoshuawuyts/choo/blob/master/scripts/instrument#L24-L35
20:06 < yoshuawuyts> gzip size tends to translate slightly better I find
20:06 < ronnross> So curious how one would use something like https://codemirror.net/ in Choo.
20:07 < ronnross> Would you work directly with the dome of use subscriptions to handle working with it.
20:07 < ronnross> Might sound like a crazy question, but when I implemented it in Elm I had to use ports and subscriptions.

TAP version 13

Platform

ok 1 (unnamed assert) Node.js 6.2.1 on Linux 64-bit

1..1

tests 1

pass 1

ok