Skip to content

Instantly share code, notes, and snippets.

@jozsefs
jozsefs / mutation-observer-example.js
Last active October 26, 2016 12:03
mutation observer example
const observer = new MutationObserver((mutations) =>
mutations.forEach((mutation) =>
console.log(`New layout: ${mutation.target.getAttribute('data-layout')}`)
)
);
observer.observe(document.body, {
attributes: true,
attributeFilter: ['data-layout']
});
@jozsefs
jozsefs / roman-converter.js
Last active March 24, 2017 11:32
roman-converter.js
const letters = ['I', 'V', 'X', 'L', 'C', 'D', 'M'];
const values = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000
};

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.

@jozsefs
jozsefs / biggest-prime-in-number.js
Last active April 7, 2017 10:34
biggest-prime-in-number.js
function isPrime(num) {
for (let i = 2, s = Math.sqrt(num); i <= s; i++) {
if (num % i === 0) {
return false;
}
}
return num !== 1;
}
function getBiggestPrimeInNumber(num) {
@jozsefs
jozsefs / biggest-prime-in-number.go
Last active April 7, 2017 12:24
biggest-prime-in-number.go
package main
import (
"fmt"
"math"
"strconv"
"log"
"sort"
"time"
"errors"
@jozsefs
jozsefs / kata-harrypotter.go
Created April 21, 2017 10:51
kata-harrypotter.go
package main
import (
"fmt"
"sort"
)
var BOOK_PRICE = 8
var priceMultiplier = map[int]float32{
@jozsefs
jozsefs / spiralize.js
Last active May 5, 2017 11:01
spiralize
const PATH_CHAR = 1;
const EMPTY_CHAR = 0;
function spiralize(size) {
const arr = createArray(size);
return generateSpiral(arr).forEach(drawLine);
}
function generateSpiral(arr) {
const len = arr.length;
@jozsefs
jozsefs / cat-mouse.js
Last active May 24, 2017 12:47
cat-mouse.js
function catMouse(map, maxMoves){
const pos = map.split('\n').reduce((acc, row, idx) => {
const catIndex = row.indexOf('C');
const mouseIndex = row.indexOf('m');
if (catIndex > -1) {
acc.cat = [idx, catIndex];
}
if (mouseIndex > -1) {
acc.mouse = [idx, mouseIndex];
@jozsefs
jozsefs / webpack.config.js
Last active June 20, 2017 06:06
webpack better vendor hashing config
module.exports = {
output: {
filename: '[name]-[chunkhash].js',
},
plugins: [
new webpack.HashedModuleIdsPlugin(),
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
minChunks: m => /node_modules/.test(m.context),
}),
@jozsefs
jozsefs / alphabet-wars.js
Last active August 24, 2017 12:18
alphabet-wars.js
function alphabetWar(battlefield) {
const remaining = [];
const splitStr = battlefield.split(/(\[[^\]]*\])/g);
const hasNuke = battlefield.indexOf('#') > -1;
splitStr.forEach((item, idx) => {
const isInShelter = item.indexOf('[') === 0;
// the whole thing was put together in like 20 minutes #ripReadability
if (!hasNuke || isInShelter && (