Skip to content

Instantly share code, notes, and snippets.

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.

@codyaray
codyaray / invite_to_slack.js
Last active December 15, 2017 20:57 — forked from dommmel/invite_to_slack.js
Zapier Code to auto invite users to slack
var slackTeam = "YOUR_SLACK_TEAM_NAME ";
var token = 'YOUR_ADMIN_TEST_TOKEN';
var channels = 'C1234567890,G12345678';
// A test token will suffice.
// You can generate one at https://api.slack.com/docs/oauth-test-tokens
// Just make sure that the user issuing the test token is an admin.
var url = 'https://'+ slackTeam + '.slack.com/api/users.admin.invite';
fetch(url, {