Chokepoint: Rate Limiter

This library provides throttling for arbitary blocks. Chokepoint is based on Arto Bendiken's rack-throttle library.

Features

  • Throttles a block by enforcing a minimum time interval between subsequent calls or by enforcing a maximum number of calls in a given duration (Daily, Hourly, Minute)
  • Stores rate-limiting counters in any key/value store implementation that responds to #[]/#[]= (like Ruby's hashes) or to #get/#set (like memcached or Redis).
  • Compatible with the gdbm binding included in Ruby's standard library.
  • Compatible with the memcached, memcache-client, memcache and redis gems.
  • Compatible with Heroku's memcached add-on.
  • Compatible with Ruby 1.8.7 & 1.9

Examples

Enforcing a minimum 3-second interval between calls

Chokepoint::Interval('my block', :min => 3.0).throttle do
  ...
end

Allowing a maximum of 60 requests per minute

Chokepoint::Minute('my block', :max => 60).throttle do
  ...
end

Storing the rate-limiting counters on a Memcached server

require 'memcached'

Checkpoint::Interval.new('my block', :cache => Memcached.new, :key_prefix => :throttle) do
  ...
end

Throttling Strategies

Chokepoint supports four built-in throttling strategies:

  • Chokepoint::Interval: Throttles the application by enforcing a minimum interval (by default, 1 second) between calls.
  • Chokepoint::Minute: Throttles the application by defining a maximum number of allowed calls per minute (by default, 60).
  • Chokepoint::Hourly: Throttles the application by defining a maximum number of allowed calls per hour (by default, 3,600).
  • Chokepoint::Daily: Throttles the application by defining a maximum number of allowed calls per day (by default, 86,400).

You can fully customize the implementation details of any of these strategies by simply subclassing one of the aforementioned default implementations. And, of course, should your application-specific requirements be significantly more complex than what we've provided for, you can also define entirely new kinds of throttling strategies by subclassing the Chokepoint::Limiter base class directly.

Authors

Based on rack-throttle work by Arto Bendiken and Brendon Murphy

License

Distribution is allowed under the MIT License.