Skip to content

Instantly share code, notes, and snippets.

View IanField90's full-sized avatar
:shipit:

Ian Field IanField90

:shipit:
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.

/*
* Copyright 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@kaushikgopal
kaushikgopal / RxSchedulerHook.java
Created July 8, 2016 16:50
Lazy man's RxJava Espresso Scheduler Hooks
public class RxSchedulerHook {
private ISRxSchedulerHook() {
// no instances
}
/**
* this makes sure that when we run the tests all of RxJava
* operates on a single thread (Scheduler.immediate)
*/
@IanField90
IanField90 / AppComponent.java
Last active March 24, 2016 17:02
Basic AndroidApplication class
@Singleton @Component(modules = AppModule.class) public interface AppComponent {
void inject(MainActivity activity);
}
@IanField90
IanField90 / Gemfile
Created March 23, 2016 13:30
Helpful ruby gems
gem 'ruport'
gem 'acts_as_reportable'
gem 'state_machine'
# use ap instead of p to print nicer
gem 'awesome_print'
gem 'acts_as_api'
gem 'rolify'
@IanField90
IanField90 / wifi adb.md
Last active March 1, 2016 08:48
WiFi adb

Connect via usb then:

adb tcpip 5555

Disconnect

adb connect 192.168.0.17:5555 or discover by adb shell ip -f inet addr show wlan0

Resume usb mode for ADB again

adb usb

@kaushikgopal
kaushikgopal / update.sh
Last active February 1, 2022 16:36
My morning cli ritual
echo "y" | android update sdk --no-ui; echo "yes" | apm upgrade; softwareupdate -i -a; brew update; brew upgrade; brew cleanup; brew cask cleanup; npm update npm -g; npm update -g; gem update
@wvengen
wvengen / README.md
Last active March 25, 2024 07:53
Ruby memory analysis over time

Finding a Ruby memory leak using a time analysis

When developing a program in Ruby, you may sometimes encounter a memory leak. For a while now, Ruby has a facility to gather information about what objects are laying around: ObjectSpace.

There are several approaches one can take to debug a leak. This discusses a time-based approach, where a full memory dump is generated every, say, 5 minutes, during a time that the memory leak is showing up. Afterwards, one can look at all the objects, and find out which ones are staying around, causing the

@kevinelliott
kevinelliott / osx-10.11-setup.md
Last active April 10, 2022 15:47
Mac OS X 10.11 El Capitan Setup

Mac OS X 10.11 El Capitan

Custom recipe to get OS X 10.11 El Capitan running from scratch, setup applications and developer environment. This is very similar (and currently mostly the same) as my 10.10 Yosemite setup recipe (as found on this gist https://gist.github.com/kevinelliott/0726211d17020a6abc1f). Note that I expect this to change significantly as I install El Capitan several times.

I use this gist to keep track of the important software and steps required to have a functioning system after a semi-annual fresh install. On average, I reinstall each computer from scratch every 6 months, and I do not perform upgrades between distros.

This keeps the system performing at top speeds, clean of trojans, spyware, and ensures that I maintain good organizational practices for my content and backups. I highly recommend this.

You are encouraged to fork this and modify it to your heart's content to match your own needs.

@leohoo
leohoo / NullBodySafeClient.java
Last active December 9, 2020 11:13
A retrofit client can POST empty body.
import com.squareup.okhttp.OkHttpClient;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import retrofit.client.Header;
import retrofit.client.OkClient;
import retrofit.client.Request;
import retrofit.client.Response;