The project is in a healthy, maintained state
This gem provides a simple and flexible rate limiting mechanism for Rack applications. It allows you to control the rate at which requests are processed, preventing abuse and ensuring fair usage.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

>= 0
~> 13.0
~> 3.10
~> 0.9.0

Runtime

~> 3.0
~> 2.0
~> 4.0
 Project Readme

RateLimiter

A flexible rate limiting gem for Rack-based Ruby applications. Provides multiple storage options and easy configuration for API rate limiting.

Installation

Install the gem and add to the application's Gemfile by executing:

bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG

Alternatively, add this line to your application's Gemfile:

gem 'UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG'

And then execute:

$ bundle install

If bundler is not being used to manage dependencies, install the gem by executing:

gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG

Usage

Basic Setup

Add the middleware to your application:

For Rails Applications

# config/application.rb
module YourApp
  class Application < Rails::Application
    config.middleware.use RateLimiter::Middleware
  end
end

For Rack Applications

# config.ru
require 'rate_limiter'

RateLimiter.configure do |config|
  config.requests_per_minute = 100
end

use RateLimiter::Middleware
run YourApp

Configuration

Configure the rate limiter with your desired settings:

# config/initializers/rate_limiter.rb (for Rails)
RateLimiter.configure do |config|
  config.requests_per_minute = 100  # Number of requests allowed per minute
  config.storage_type = :memory     # Choose storage type (:memory, :redis, :memcache)

  # Redis configuration (if using Redis storage)
  config.redis_options = {
    host: 'localhost',
    port: 6379
  }
end

Storage Options

The gem supports multiple storage backends:

Memory Storage (Default)

RateLimiter.configure do |config|
  config.storage_type = :memory
end

Memcache Storage

RateLimiter.configure do |config|
  config.storage_type = :memcache
end

Redis Storage

RateLimiter.configure do |config|
  config.storage_type = :redis
  config.redis_options = {
    host: 'localhost',
    port: 6379
  }
end

Rate Limit Exceeded Response

When the rate limit is exceeded, the middleware returns:

  • Status: 429 Too Many Requests
  • Body: "Rate limit exceeded. Try again later."

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also 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.

Contributing

TODO: Update the github url below Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rate_limiter.

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

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the RateLimiter project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.