class HttpThreshold::Throttle

Constants

MANDATORY_OPTIONS

Attributes

block[R]
limit[R]
name[R]
period[R]
type[R]

Public Class Methods

new(name, options) click to toggle source
# File lib/http_threshold/throttle.rb, line 6
def initialize(name, options)
  @name = name
  MANDATORY_OPTIONS.each do |opt|
    raise ArgumentError.new("Must pass #{opt.inspect} option") unless options[opt]
  end
  @limit  = options[:limit]
  @period = options[:period].to_i
end

Public Instance Methods

cache() click to toggle source
# File lib/http_threshold/throttle.rb, line 15
def cache
  HttpThreshold::Client.cache
end
incr_count() click to toggle source

similar as method []

# File lib/http_threshold/throttle.rb, line 24
def incr_count
  begin
    result = lock_manager.lock!("lock:#{name}", period) do
      if reach_limit?
        false
      else
        cache.count(threshold_key, period)
        true
      end
    end
    result
  rescue Redlock::LockError
    sleep 0.25
    retry
  end
end
lock_manager() click to toggle source
# File lib/http_threshold/throttle.rb, line 19
def lock_manager
  HttpThreshold::Client.lock_manager
end
reach_limit?() click to toggle source
# File lib/http_threshold/throttle.rb, line 41
def reach_limit?
  count = cache.read_count(threshold_key, period)
  count >= limit
end
status() click to toggle source
# File lib/http_threshold/throttle.rb, line 46
def status
  count = cache.read_count(threshold_key, period)
  "#{count}/#{limit}"
end
threshold_key() click to toggle source
# File lib/http_threshold/throttle.rb, line 51
def threshold_key
  "threshold:#{name}"
end