Skip to content

Instantly share code, notes, and snippets.

View bryanrite's full-sized avatar
👨‍💻

Bryan Rite bryanrite

👨‍💻
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.

# config/routes.rb
resources :documents do
scope module: 'documents' do
resources :versions do
post :restore, on: :member
end
resource :lock
end
end
@bryanrite
bryanrite / development.rb
Last active September 22, 2015 11:37 — forked from chrisyour/development.rb
Limit the size of your development.log and test.log files in your Rails 4 and Rails 3 apps.
# Add this to config/environments/development.rb
# Limit your development log file to 5 MB
config.logger = Logger.new(config.paths["log"].first, 1, 5242880) # 5 megabytes

TLDR

This:

ffmpeg -i in.mov -pix_fmt rgb24 -r 24 -f gif - | gifsicle --optimize=3 --delay=3 > out.gif

Is generally nice with a Quicktime Player screen cap.

OS X Screencast to animated GIF

@bryanrite
bryanrite / safe.md
Last active October 24, 2024 16:27
Safe Postgres Operations on High Volume Tables

Originally taken from: Braintree Article and expanded on by me.

Safe

  • Add a new column
  • Drop a column
  • Rename a column
  • Add an index concurrently (Example), Note: it will still take a long time to run the migration, but it won't write-lock the table.
  • Drop a constraint (for example, non-nullable)
  • Add a default value to an existing column

Keybase proof

I hereby claim:

  • I am bryanrite on github.
  • I am bryanrite (https://keybase.io/bryanrite) on keybase.
  • I have a public key whose fingerprint is B30B 6C25 8639 D831 DD61 3D57 ACE2 A309 51FA B65E

To claim this, I am signing this object:

@bryanrite
bryanrite / pull-requests.bash
Last active January 4, 2016 17:39
Upstream Pull-Requests into My fork.
# You have forked a project but someone has submitted a nice looking pull-request
# to the upstream project that you want for your fork.
# Setup upstream repo
git remote add upstream https://github.com/whatever/repo.git
# Get the desired pull-request from upstream
git fetch upstream pull/<PR-ID>/head:pr-<PR-ID>
# Rebase pull-request on your master