class ExcessFlow::FixedWindowStrategy

ExcessFlow::FixedWindowStrategy

Definition of fixed window rate limiting strategy and it's behaviour implementations. Fixed window allows only N requests for a given key to be done in a O time window where O start is defined at the time of first request. Window expiration starts with the first request for a given key. Once expired it will reset back to 0.

Public Instance Methods

within_rate_limit?() click to toggle source
# File lib/excess_flow/fixed_window_strategy.rb, line 26
def within_rate_limit?
  ExcessFlow::GlobalMutex.locked(lock_key: configuration.lock_key) do
    if current_requests < configuration.limit
      bump_counter
      start_expiration_window

      true
    else
      false
    end
  end
end

Private Instance Methods

bump_counter() click to toggle source
# File lib/excess_flow/fixed_window_strategy.rb, line 41
def bump_counter
  ExcessFlow.redis { |r| r.incr(configuration.counter_key) }
end
current_requests() click to toggle source
# File lib/excess_flow/fixed_window_strategy.rb, line 45
def current_requests
  @current_requests ||= ExcessFlow.redis { |r| r.get(configuration.counter_key) }.to_i
end
current_ttl() click to toggle source
# File lib/excess_flow/fixed_window_strategy.rb, line 49
def current_ttl
  @current_ttl ||= ExcessFlow.redis { |r| r.ttl(configuration.counter_key) }.to_i
end
start_expiration_window() click to toggle source
# File lib/excess_flow/fixed_window_strategy.rb, line 53
def start_expiration_window
  return unless current_ttl.negative?

  ExcessFlow.redis { |r| r.expire(configuration.counter_key, configuration.ttl) }
end