Rate Limiter is a rails module that can easily limit requests per client for all endpoints or for specific ones.
To add it, all you have to do is include RateLimit in ApplicationController to have it on all your controllers using default settings of 100 request per hour (3600 seconds).
You can also add it directly to the controller you want to be rate limited.
To customize, just use helper methods rate and period, as following:
class AnImportantController < ApplicationController
include RateLimit
# those are the default (optional declaration)
rate 100 # one hundred request
period 3600 # one hour
…
endEasy run with docker (recommended) or run directly in the system.
Download rvm
\curl -sSL https://get.rvm.io | bash -s stable --railsInstall ruby 2.6.5
rvm install 2.6.5Install rails
gem install railsBundle
bundle installHave PostgreSQL installed (binaries available on: https://www.postgresql.org/download/)
Set credentials
mv config/database.yml config/database.yml.bak
mv config/database.pg.yml config/database.ymlFill specific DB settings like PG user/role, port and address, if you don't have the typical defaults.
Setup the database
rails db:createRun the project with
rails sOpen localhost:3000/low_rate/index and refresh twice to see yourself blocked.
Simply run
rails thttps://hub.docker.com/?overlay=onboarding
https://hub.docker.com/?overlay=onboarding
https://docs.docker.com/compose/install/
Run the commands:
docker-compose build
docker-compose upIn another terminal, run:
docker-compose run web rails db:createOpen localhost:3000/low_rate/index and refresh twice to see yourself blocked.
While server is up (docker-compose up), run:
docker-compose run web rails t