https://github.com/rnd-soft/rapidity
Simple but fast Redis-backed distributed rate limiter. Allows you to specify time interval and count within to limit distributed operations.
https://github.com/rnd-soft/rapidity
gem rate-limiting redis ruby
Last synced: about 1 year ago
JSON representation
Simple but fast Redis-backed distributed rate limiter. Allows you to specify time interval and count within to limit distributed operations.
- Host: GitHub
- URL: https://github.com/rnd-soft/rapidity
- Owner: RND-SOFT
- License: mit
- Created: 2022-06-29T06:45:26.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2023-04-03T15:07:54.000Z (about 3 years ago)
- Last Synced: 2025-04-03T18:40:55.903Z (about 1 year ago)
- Topics: gem, rate-limiting, redis, ruby
- Language: Ruby
- Size: 31.3 KB
- Stars: 5
- Watchers: 5
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Rapidity
[](https://rubygems.org/gems/rapidity)
[](https://rubygems.org/gems/rapidity/versions)
[](http://www.rubydoc.info/gems/rapidity)
[](https://lysander.rnds.pro/api/v1/badges/rapidity_coverage.html)
[](https://lysander.rnds.pro/api/v1/badges/rapidity_quality.html)
[](https://lysander.rnds.pro/api/v1/badges/rapidity_outdated.html)
[](https://lysander.rnds.pro/api/v1/badges/rapidity_vulnerable.html)
Simple but fast Redis-backed distributed rate limiter. Allows you to specify time interval and count within to limit distributed operations.
Features:
- extremly simple
- free from race condition through LUA scripting
- fast
[Article(russian) about gem.](https://blog.rnds.pro/029-rapidity/?utm_source=github&utm_medium=repo&utm_campaign=rnds)
## Usage
Rapidity has two variants:
- simple `Rapidity::Limiter` to handle single distibuted counter
- complex `Rapidity::Composer` to handle multiple counters at once
### Single conter with concurrent access
```ruby
pool = ConnectionPool.new(size: 10) do
Redis.new(url: ENV.fetch('REDIS_URL', 'redis://127.0.0.1:6379'))
end
# allow no more 10 requests within 5 seconds
limiter = Rapidity::Limiter.new(pool, name: 'requests', threshold: 10, interval: 5)
loop do
# try to obtain 3 requests at once
quota = limiter.obtain(3).times do
make_request
end
if quota == 0
# no more requests allowed within interval
sleep 1
end
end
```
### Multiple counters
```ruby
pool = ConnectionPool.new(size: 10) do
Redis.new(url: ENV.fetch('REDIS_URL', 'redis://127.0.0.1:6379'))
end
LIMITS = [
{ interval: 1, threshold: 2 }, # no more 2 requests per second
{ interval: 60, threshold: 200 }, # no more 200 requests per minute
{ interval: 86400, threshold: 10000 } # no more 10k requests per day
]
limiter = Rapidity::Composer.new(pool, name: 'requests', limits: LIMITS)
loop do
# try to obtain 3 requests at once
quota = limiter.obtain(3).times do
make_request
end
if quota == 0
# no more requests allowed within interval
puts limiter.remains # inspect current limits
sleep 1
end
end
```
## Installation
It's a gem:
```bash
gem install rapidity
```
There's also the wonders of [the Gemfile](http://bundler.io):
```ruby
gem 'rapidity'
```
## Special Thanks
- [WeTransfer/prorate](https://github.com/WeTransfer/prorate) for LUA-examples