class RedisRateLimiter

Attributes

interval[RW]

@!attribute key

@return [String] Name which uniquely identifies this rate limiter

@!attribute redis

@return [Redis] Redis client associated with this rate limiter

@!attribute interval

@return [Integer] Time span this rate limiter tracks in seconds

@!attribute limit

@return [Integer] Max count allowed by rate limiter in interval
key[RW]

@!attribute key

@return [String] Name which uniquely identifies this rate limiter

@!attribute redis

@return [Redis] Redis client associated with this rate limiter

@!attribute interval

@return [Integer] Time span this rate limiter tracks in seconds

@!attribute limit

@return [Integer] Max count allowed by rate limiter in interval
limit[RW]

@!attribute key

@return [String] Name which uniquely identifies this rate limiter

@!attribute redis

@return [Redis] Redis client associated with this rate limiter

@!attribute interval

@return [Integer] Time span this rate limiter tracks in seconds

@!attribute limit

@return [Integer] Max count allowed by rate limiter in interval
redis[RW]

@!attribute key

@return [String] Name which uniquely identifies this rate limiter

@!attribute redis

@return [Redis] Redis client associated with this rate limiter

@!attribute interval

@return [Integer] Time span this rate limiter tracks in seconds

@!attribute limit

@return [Integer] Max count allowed by rate limiter in interval

Public Class Methods

new(key, redis, options = {}) click to toggle source

Initializes a new RedisRateLimiter object

@param [String] key A name to uniquely identify this rate limiter @param [Redis] redis Redis client associated with rate limiter @param options [Integer] :interval Time span to track in seconds @param options [Integer] :limit Max count allowed in interval @return [RedisRateLimiter] Instance of this rate limiter

# File lib/redis_rate_limiter.rb, line 22
def initialize key, redis, options = {}
  @key      = key
  @redis    = redis
  @limit    = options[:limit] || 50
  @interval = options[:interval] || 60
end

Public Instance Methods

add(subject, time = Time.now.to_f) click to toggle source

Add to subject's count

@param [String] subject A name to uniquely identify subject @param [time] time UNIX timestamp of event

# File lib/redis_rate_limiter.rb, line 33
def add subject, time = Time.now.to_f
  subject_key = "#{@key}:#{subject}"
  @redis.multi do
    @redis.lpush(subject_key, time)
    @redis.ltrim(subject_key, 0, @limit - 1)
    @redis.expire(subject_key, @interval)
  end
end
count(subject) click to toggle source

Get number of events currently recorded for subject

@param [String] subject Name which uniquely identifies subject @return [Integer] Returns number of events currently recorded for subject

# File lib/redis_rate_limiter.rb, line 67
def count subject
  @redis.llen("#{@key}:#{subject}")
end
exceeded?(subject) click to toggle source

Check if subject has exceeded count

@param [String] subject Name which uniquely identifies subject @return [Boolean] Returns true if subject has exceeded count

# 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) click to toggle source

Get time in seconds until subject is not rate limited

@param [String] subject Name which uniquely identifies subject @return [Float] Returns time in seconds until subject is not rate limited

# 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

Private Instance Methods

time_since_oldest(subject_key) click to toggle source
# File lib/redis_rate_limiter.rb, line 73
def time_since_oldest subject_key
  Time.now.to_f - @redis.lindex(subject_key, -1).to_f
end