Rack::Congestion

Build Status Test Coverage Code Climate

Rack middleware for Congestion

Provides rate limiting for Rack-based applications.

Installation

Add this line to your application's Gemfile:

gem 'rack-congestion'

And then execute:

$ bundle

Or install it yourself as:

$ gem install rack-congestion

Usage

The available limiters are:

  • Rack::Congestion::Limiter
    • Application-wide limiting
  • Rack::Congestion::IpLimiter
    • Limits requests per-IP
  • Rack::Congestion::PathLimiter
    • Limits requests for a path segment
    • e.g. limit requests to '/api'
    • accepts path:(required) and path_matcher:(optional) options

Examples and more advanced usage can be found in examples

Documentation of Congestion configuration can be found here

In a Rack application

require 'rack/congestion'

# Limit requests to
#   - a maximum of 100 requests per minute
#   - a maximum rate of 1 request every 100 milliseconds
use Rack::Congestion::IpLimiter, interval: 60, max_in_interval: 100, min_delay: 0.1

run ->(env){
  [200, { 'Content-Type' => 'text/plain' }, ['Hello world']]
}

In a Rails application

# config/application.rb
require 'rack/congestion'

class YourApplication < Rails::Application
  config.middleware.use Rack::Congestion::IpLimiter
end

Development

After checking out the repo, run bin/setup to install dependencies. Then, run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To run the specs, run bundle exec rake.

Contributing

  1. Fork it ( https://github.com/parrish/rack-congestion/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request