class Rack::Defense::ThrottleCounter

Constants

KEY_PREFIX
SCRIPT

Attributes

name[RW]

Public Class Methods

new(name, max_requests, time_period, store) click to toggle source
# File lib/rack/defense/throttle_counter.rb, line 8
def initialize(name, max_requests, time_period, store)
  @name, @max_requests, @time_period = name.to_s, max_requests.to_i, time_period.to_i
  raise ArgumentError, 'name should not be nil or empty' if @name.empty?
  raise ArgumentError, 'max_requests should be greater than zero' unless @max_requests > 0
  raise ArgumentError, 'time_period should be greater than zero' unless @time_period > 0
  @store = store
end

Public Instance Methods

throttle?(key, timestamp=nil) click to toggle source
# File lib/rack/defense/throttle_counter.rb, line 16
def throttle?(key, timestamp=nil)
  timestamp ||= (Time.now.utc.to_f * 1000).to_i
  @store.eval SCRIPT,
    ["#{KEY_PREFIX}:#{@name}:#{key}"],
    [timestamp, @max_requests, @time_period]
end