Class: RedisRateLimiter
- Inherits:
-
Object
- Object
- RedisRateLimiter
- Defined in:
- lib/redis_rate_limiter.rb,
lib/redis_rate_limiter/version.rb
Constant Summary collapse
- VERSION =
'0.2.0'.freeze
Instance Attribute Summary collapse
-
#interval ⇒ Integer
Time span this rate limiter tracks in seconds.
-
#key ⇒ String
Name which uniquely identifies this rate limiter.
-
#limit ⇒ Integer
Max count allowed by rate limiter in interval.
-
#redis ⇒ Redis
Redis client associated with this rate limiter.
Instance Method Summary collapse
-
#add(subject, time = Time.now.to_f) ⇒ Object
Add to subject’s count.
-
#count(subject) ⇒ Integer
Get number of events currently recorded for subject.
-
#exceeded?(subject) ⇒ Boolean
Check if subject has exceeded count.
-
#initialize(key, redis, options = {}) ⇒ RedisRateLimiter
constructor
Initializes a new RedisRateLimiter object.
-
#retry_in?(subject) ⇒ Float
Get time in seconds until subject is not rate limited.
Constructor Details
#initialize(key, redis, options = {}) ⇒ RedisRateLimiter
Initializes a new RedisRateLimiter object
22 23 24 25 26 27 |
# File 'lib/redis_rate_limiter.rb', line 22 def initialize key, redis, = {} @key = key @redis = redis @limit = [:limit] || 50 @interval = [:interval] || 60 end |
Instance Attribute Details
#interval ⇒ Integer
Returns Time span this rate limiter tracks in seconds.
13 |
# File 'lib/redis_rate_limiter.rb', line 13 attr_accessor :key, :redis, :limit, :interval |
#key ⇒ String
Returns Name which uniquely identifies this rate limiter.
13 14 15 |
# File 'lib/redis_rate_limiter.rb', line 13 def key @key end |
#limit ⇒ Integer
Returns Max count allowed by rate limiter in interval.
13 |
# File 'lib/redis_rate_limiter.rb', line 13 attr_accessor :key, :redis, :limit, :interval |
#redis ⇒ Redis
Returns Redis client associated with this rate limiter.
13 |
# File 'lib/redis_rate_limiter.rb', line 13 attr_accessor :key, :redis, :limit, :interval |
Instance Method Details
#add(subject, time = Time.now.to_f) ⇒ Object
Add to subject’s count
33 34 35 36 37 38 39 40 |
# File 'lib/redis_rate_limiter.rb', line 33 def add subject, time = Time.now.to_f subject_key = "#{@key}:#{subject}" @redis.multi do |pipeline| pipeline.lpush(subject_key, time) pipeline.ltrim(subject_key, 0, @limit - 1) pipeline.expire(subject_key, @interval) end end |
#count(subject) ⇒ Integer
Get number of events currently recorded for subject
67 68 69 |
# File 'lib/redis_rate_limiter.rb', line 67 def count subject @redis.llen("#{@key}:#{subject}") end |
#exceeded?(subject) ⇒ Boolean
Check if subject has exceeded count
46 47 48 49 50 |
# File 'lib/redis_rate_limiter.rb', line 46 def exceeded? subject subject_key = "#{@key}:#{subject}" return false if @redis.llen(subject_key) < @limit time_since_oldest(subject_key) < @interval end |
#retry_in?(subject) ⇒ Float
Get time in seconds until subject is not rate limited
56 57 58 59 60 61 |
# File 'lib/redis_rate_limiter.rb', line 56 def retry_in? subject subject_key = "#{@key}:#{subject}" return 0.0 if @redis.llen(subject_key) < @limit elapsed = time_since_oldest(subject_key) elapsed > @interval ? 0.0 : @interval - elapsed end |