Skip to content

Instantly share code, notes, and snippets.

View Chetan11-dev's full-sized avatar
💭
I may be slow to respond so please give at least 1 week.

Chetan Jain Chetan11-dev

💭
I may be slow to respond so please give at least 1 week.
View GitHub Profile
@Chetan11-dev
Chetan11-dev / AssetDatabaseOpenHelper.java
Created August 6, 2019 10:33 — forked from wontondon/AssetDatabaseOpenHelper.java
Copy sqlite database from assets dir - Android
package com.javatarts.basketballgm.data;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

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.