šŸš€ Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more →
Socket
Sign inDemoInstall
Socket

redis-throttle

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

redis-throttle

2.0.1
Rubygems
Version published
Maintainers
1
Created
Source

= 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

[source,ruby]

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

[source,ruby]

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

[source,ruby]

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:

[source,ruby]

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:

  • https://www.ruby-lang.org[Ruby] ** MRI 2.7.x ** MRI 3.0.x ** MRI 3.1.x ** MRI 3.2.x
  • https://redis.io[Redis Server] ** 6.0.x ** 6.2.x ** 7.0.x
  • https://github.com/redis/redis-rb[redis-rb] ** 4.1.x ** 4.2.x ** 4.3.x ** 4.4.x ** 4.5.x ** 4.6.x ** 4.7.x ** 4.8.x ** 5.0.x
  • https://github.com/resque/redis-namespace[redis-namespace] ** 1.10.x
  • https://github.com/redis-rb/redis-client[redis-client] ** 0.12.x ** 0.13.x ** 0.14.x

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:

FAQs

Package last updated on 19 Oct 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts