Throttle allows you to rate limit based on a number of strategies. Currently two strategies are implemented, 'interval' and 'timespan'. Using an 'interval' you could limit the number of requests you receive and using a 'timespan' you can set a max around hourly and daily settings (planned for the future to be configurable).
Add this line to your application's Gemfile:
gem 'throttle'
And then execute:
$ bundle
Or install it yourself as:
$ gem install throttle
Example of default setup:
Throttle.setup
Example of connecting to an external Redis
Throttle.setup(Redis.new(...))
Example of working with the default limits set above (only one default limit can be specified, either timespan or interval:
Throttle.limited?(:default)
Example of a timespan strategy (required keys: max, timespan):
Throttle.set_limit(:apihits, {:max => 1000, :timespan => "daily"})
Example of interval strategy (required keys: interval):
Throttle.set_limit(:apirate, {:interval => 3.0})
Example of a temporary interval strategy using blocks (not currently implemented):
Throttle.set_limit(:apirate, {:interval => 3.0}) do
# do work
end
Example of checking whether we are limited:
Throttle.limited?(:apihits)
Throttle.limited?(:apirate)
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request