Skip to content

redis-throttle ruby gem fork

License

Notifications You must be signed in to change notification settings

doctolib/redis-throttle

Repository files navigation

RedisThrottle

Redis based rate limit and concurrency throttling.

Installation

Add this line to your application’s Gemfile:

$ bundle add redis-throttle

Or install it yourself as:

$ gem install redis-throttle

Usage

Concurrency Limit

# Allow 1 concurrent calls. If call takes more than 10 seconds, consider it
# gone (as if process died, or by any other reason did not called `#release`):
concurrency = RedisThrottle.concurrency(:bucket_name,
  limit: 1,
  ttl:   10
)

concurrency.acquire(redis, token: "abc") # => "abc"
concurrency.acquire(redis, token: "xyz") # => nil

concurrency.release(redis, token: "abc")

concurrency.acquire(redis, token: "xyz") # => "xyz"

Rate Limit

# Allow 1 calls per 10 seconds:
rate_limit = RedisThrottle.rate_limit(:bucket_name,
  limit:  1,
  period: 10
)

rate_limit.acquire(redis) # => "6a6c6546-268d-4216-bcf3-3139b8e11609"
rate_limit.acquire(redis) # => nil

sleep 10

rate_limit.acquire(redis) # => "e2926a90-2cf4-4bff-9401-65f3a70d32bd"

Multi-strategy

throttle = RedisThrottle
  .concurrency(:db, limit: 3, ttl: 900)
  .rate_limit(:api_minutely, limit: 1, period: 60)
  .rate_limit(:api_hourly, limit: 10, period: 3600)

throttle.call(redis, token: "abc") do
  # do something if all strategies are resolved
end

You can also compose multiple throttlers together:

db_limiter  = RedisThrottle.concurrency(:db, limit: 3, ttl: 900)
api_limiter = RedisThrottle
  .rate_limit(:api_minutely, limit: 1, period: 60)
  .rate_limit(:api_hourly, limit: 10, period: 3600)

(db_limiter + api_limiter).call(redis) do
  # ...
end

Compatibility

This library aims to support and is tested against:

If something doesn’t work on one of these versions, it’s a bug.

This library may inadvertently work (or seem to work) on other Ruby versions, however support will only be provided for the versions listed above.

If you would like this library to support another Ruby version or implementation, you may volunteer to be a maintainer. Being a maintainer entails making sure all tests run and pass on that implementation. When something breaks on your implementation, you will be responsible for providing patches in a timely fashion. If critical issues for a particular implementation exist at the time of a major release, support for that Ruby version may be dropped.

The same applies to Redis Server, redis-rb, redis-namespace, and redis-client support.

Development

scripts/update-gemfiles
scripts/run-rspec
bundle exec rubocop

Contributing

  • Fork redis-throttle

  • Make your changes

  • Ensure all tests pass (bundle exec rake)

  • Send a merge request

  • If we like them we’ll merge them

  • If we’ve accepted a patch, feel free to ask for commit access!

Appreciations

Thanks to all how providede suggestions and criticism, especially to those who helped me shape some of the initial ideas: