class Rolling::Limit

Rolling::Limit is a class that maintains a rolling limit of no more than <max_operations> operations for a given <key> within <timespan> seconds.

Constants

VERSION

Attributes

key[R]
max_operations[R]
redis[R]
timespan[R]

Public Class Methods

new(redis:, key:, max_operations:, timespan:) click to toggle source
# File lib/rolling/limit.rb, line 8
def initialize(redis:, key:, max_operations:, timespan:)
  @redis = redis
  @key = "rlmt:#{key}"
  @timespan = timespan
  @max_operations = max_operations.to_i
end

Public Instance Methods

remaining() click to toggle source

Increments the counter and returns truthy (with number of remaining operations)

# File lib/rolling/limit.rb, line 17
def remaining
  cleanup
  return false if count >= max_operations
  increment
  max_operations - count
end
remaining?() click to toggle source

Shows numbers of remaining operations without incrementing

# File lib/rolling/limit.rb, line 30
def remaining?
  cleanup
  return false if count >= max_operations
  true
end
reset!() click to toggle source

Resets this counter

# File lib/rolling/limit.rb, line 25
def reset!
  redis.del(key)
end

Private Instance Methods

autoexpire() click to toggle source

Ensure this key lives for at least <timespan>

# File lib/rolling/limit.rb, line 41
def autoexpire
  redis.expire(key, timespan)
end
cleanup() click to toggle source

Delete operations that are older than time - timespan

# File lib/rolling/limit.rb, line 46
def cleanup
  redis.zremrangebyscore(key, '-inf', time - timespan)
end
count() click to toggle source
# File lib/rolling/limit.rb, line 58
def count
  redis.zcard(key)
end
increment() click to toggle source
# File lib/rolling/limit.rb, line 62
def increment
  redis.zadd(key, time, unique_key)
  autoexpire
end
time() click to toggle source
# File lib/rolling/limit.rb, line 50
def time
  Time.now.to_i
end
unique_key() click to toggle source
# File lib/rolling/limit.rb, line 54
def unique_key
  "#{Time.now.to_f}.#{rand}"
end