百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

redis-throttle

> 编程语言
开源

用于限制入站 HTTP 请求的机架中间件,已配置为与 Redis 结合使用。

273 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

用于限制入站 HTTP 请求的机架中间件,已配置为与 Redis 结合使用。

Redis Throttle Middleware

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.

Thanks to Open Source

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.

Features

  • Works only with Redis.
  • Automatically deploy by setting ENV['REDIS_RATE_LIMIT_URL'].
  • When the Redis connection is not available redis throttle skips the rate limit check (it does not blow up).
  • Automatically adds X-RateLimit-Limit and X-RateLimit-Remaining headers.
  • Set MockRedis while running your tests

Requirements

Redis Throttle is tested against MRI 1.9.3, 2.0, and 2.1.x.

Installation

Update your gem file and run bundle

ruby
gem 'redis-throttle', git: 'git://github.com/lelylan/redis-throttle.git'

Rails Example

ruby
# 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"] }

Customizations

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:

  • We want to use Doorkeper as authorization system (OAuth2)
  • The number of daily requests are based on the user id and not the IP address (default in Rack::RedisThrottle)
  • The number of daily requests is dynamically set per user by the user#rate_limit field.

Now subclass Rack::RedisThrottle::Daily, create your own rules and use it in your Rails app

ruby
# /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)
  }
end

Notes

Testing coverage

Only 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.

Contributing

Fork the repo on github and send a pull requests with topic branches. Do not forget to provide specs to your contribution.

Running specs

  • Fork and clone the repository.
  • Run gem install bundler to get the latest for the gemset.
  • Run bundle install for dependencies.
  • Run bundle exec guard and press enter to execute all specs.

Spec guidelines

Follow betterspecs.org guidelines.

Coding guidelines

Follow github guidelines.

Feedback

Use the issue tracker for bugs. Mail or Tweet us for any idea that can improve the project.

Links

  • GIT Repository
  • Initial inspiration from Martinciu's dev blog

Authors

Andrea Reginato Thanks to Lelylan for letting me share the code.

Contributors

Special thanks to the following people for submitting patches.

Changelog

See CHANGELOG

Copyright

Redis Throttle is free and unencumbered public domain software. See LICENCE

Issues· 4 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言