0.0
No commit activity in last 3 years
No release in over 3 years
The gem redis_knock implements a HTTP Throttle Control engine using Redis to store rate-limiting IP's.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
>= 0

Runtime

>= 0
 Project Readme

Ruby HTTP Throttle Control Engine using Redis

This gem implements a simple HTTP throttle control using Redis as database engine to store rate-limiting IP's.

Why not rack-throttle?

The gem rack-throttle works with the same principle and does very well the job, but stores IP's and never clears the database. The main goal of Redis Knock is the use of Redis expire command, that removes the key after X seconds.

Usage

Using in your Rails Controller with before_filter

# app/controllers/simple_controller.rb

require 'redis_knock'

class SimpleController < ApplicationController
  before_filter :check_throttle

  private
  def check_throttle
    control = RedisKnock::Control.new limit: 1000, interval: 1.hour, redis: { host: 'localhost', port: 6379, db: 1 }

    render(text: 'Rate limit exceeded', status: :forbidden) and return unless control.allowed?(request)
  end
end

Using as a Rack Middleware in your Rails app

# config/application.rb

class Application < Rails::Application
  config.middleware.use RedisKnock::Middleware, limit: 1000, interval: 1.hour, redis: { host: 'localhost', port: 6379, db: 1 }
end

Using in your Sinatra app

# a_sinatra_application.rb

require 'sinatra'
require 'redis_knock'

use RedisKnock::Middleware, limit: 1000, interval: 3600, redis: { host: 'localhost', port: 6379, db: 1 }

get '/' do
  'Hello World!'
end

Dependencies

  • redis gem
  • Redis server version >= 2.1

Installation

With rubygems:

$ [sudo] gem install redis_knock

Authors

License

RedisKnock is free and unencumbered pubic domain software. For more information, see http://unlicense.org/ or the accompanying UNLICENSE file.