用于限制入站 HTTP 请求的机架中间件,已配置为与 Redis 结合使用。
This is a fork of the Rack Throttle middleware
that provides logic for rate-limiting incoming HTTP requests to Rack applications using
Redis as storage system. You can use Rack::RedisThrottle with any Ruby web framework based
on Rack, including Ruby on Rails 3.0 and Sinatra. This gem was designed to experiment rate
limit with Rails 3.x and Doorkeeper.
Redis Throttle Middleware come to life thanks to the work I've made in Lelylan, an open source microservices architecture for the Internet of Things. If this project helped you in any way, think about giving us a star on Github.
ENV['REDIS_RATE_LIMIT_URL'].X-RateLimit-Limit and X-RateLimit-Remaining headers.Redis Throttle is tested against MRI 1.9.3, 2.0, and 2.1.x.
Update your gem file and run bundle
gem 'redis-throttle', git: 'git://github.com/lelylan/redis-throttle.git'# At the top of config/application.rb
require 'rack/redis_throttle'
# Inside the class of config/application.rb
class Application 'text/plain'}, "Hello, world!\n"] }You can fully customize the implementation details of any of these strategies by simply subclassing one of the default implementations.
In our example we want to reach these goals:
Rack::RedisThrottle)user#rate_limit field.Now subclass Rack::RedisThrottle::Daily, create your own rules and use it in your Rails app
# /lib/middlewares/daily_rate_limit
require 'rack/redis_throttle'
class DailyRateLimit 'application/json' }.merge(headers), [body(request).to_json] ]
end
def body(request)
{
status: 403,
method: request.env['REQUEST_METHOD'],
request: "#{request.env['rack.url_scheme']}://#{request.env['HTTP_HOST']}#{request.env['PATH_INFO']}",
description: 'Rate limit exceeded',
daily_rate_limit: max_per_window(request)
}
endOnly Rack::RedisThrottle::Daily has a test suite. We will cover all
the gem whenever I'll find more time and I'll see it being used widely.
Fork the repo on github and send a pull requests with topic branches. Do not forget to provide specs to your contribution.
gem install bundler to get the latest for the gemset.bundle install for dependencies.bundle exec guard and press enter to execute all specs.Follow betterspecs.org guidelines.
Follow github guidelines.
Use the issue tracker for bugs. Mail or Tweet us for any idea that can improve the project.
Andrea Reginato Thanks to Lelylan for letting me share the code.
Special thanks to the following people for submitting patches.
See CHANGELOG
Redis Throttle is free and unencumbered public domain software. See LICENCE
暂无开放 Issues,或尚未同步最近议题。