Skip to content

Instantly share code, notes, and snippets.

View jonathankau's full-sized avatar
🍊

Jonathan Kau jonathankau

🍊
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.

@jonathankau
jonathankau / 0_reuse_code.js
Created January 5, 2017 22:54
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console

Keybase proof

I hereby claim:

  • I am jonathankau on github.
  • I am jonathankau (https://keybase.io/jonathankau) on keybase.
  • I have a public key ASAKRXv22_OqHpdr3p20cpZ0-p6RoAummW-za-dLUOOuLQo

To claim this, I am signing this object:

@jonathankau
jonathankau / src_routes.js
Created July 29, 2016 05:55 — forked from gnz00/src_routes.js
React-Nprogress
/*! React Starter Kit | MIT License | http://www.reactstarterkit.com/ */
import React from 'react';
import Router from 'react-routing/src/Router';
import { canUseDOM } from 'fbjs/lib/ExecutionEnvironment';
import http from './core/HttpClient';
import App from './components/App';
import IndexPage from './components/IndexPage';
import NotFoundPage from './components/NotFoundPage';
@jonathankau
jonathankau / README.md
Created July 13, 2016 20:56 — forked from joshdover/README.md
Idiomatic React Testing Patterns

Idiomatic React Testing Patterns

Testing React components seems simple at first. Then you need to test something that isn't a pure interaction and things seem to break down. These 4 patterns should help you write readable, flexible tests for the type of component you are testing.

Setup

I recommend doing all setup in the most functional way possible. If you can avoid it, don't set variables in a beforeEach. This will help ensure tests are isolated and make things a bit easier to reason about. I use a pattern that gives great defaults for each test example but allows every example to override props when needed:

//Main necessary methoeds
// get the bluetooth device reference
public BluetoothAdapter findMyBT(BluetoothAdapter myBt){
myBt = BluetoothAdapter.getDefaultAdapter();
if (myBt!= null) {
Log.i("BTDEV","BT Device Found!");
Log.i("BTDEV","" + myBt.getName());
return myBt;
} else {
Log.i("BTDEV","No BT Device Found!");
@jonathankau
jonathankau / BtOsx.m
Last active August 29, 2015 14:12 — forked from crazycoder1999/BtOsx.m
//sample of a bluetooth RfComm COmmunication between a GPS and OSX.
//more information on: http://pestohacks.blogspot.it/2012/07/make-osx-talks-with-bluetooth-gps.html
//let's go on.
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSLog(@"ok, go on"); //
btDevice = nil;
IOBluetoothDeviceInquiry *ibdi = [IOBluetoothDeviceInquiry inquiryWithDelegate:self]; //inquiry, have delegates methoeds
[ibdi setUpdateNewDeviceNames:YES]; //Yes, I want also names for the bt devices found.