Rack::Attack::RateLimit
Add rate limit headers for Rack::Attack throttles.
Installation
Install the gem:
gem install 'rack-attack-rate-limit'
In your gemfile:
gem 'rack-attack-rate-limit', require: 'rack/attack/rate-limit'
And then execute:
bundle
Usage
Rack::Attack::RateLimit expects at least one Rack::Attack throttle to be defined:
Rack::Attack.throttle('my_throttle') do |req|
req.ip
end
To include rate limit headers for throttles, include the Rack::Attack::RateLimit middleware, and provide it with the names of the throttles you want to add rate limit headers for. A single throttle name can be provided as a string, while multiple throttle names must be provided as an array of strings.
For Rails 3+:
config.middleware.use Rack::Attack::RateLimit, throttle: ['my_throttle', 'my_other_throttle']
Rate limit headers are:
- 'X-RateLimit-Limit' - The total number of requests allowed.
- 'X-RateLimit-Remaining' - The number of remaining requests.
If a request triggers multiple throttles, the gem will add headers for the throttle with the lowest number of remaining requests.
Contributing
- 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