Class: ExcessFlow::SlidingWindowStrategy

Inherits:
Strategy
  • Object
show all
Defined in:
lib/excess_flow/sliding_window_strategy.rb

Overview

ExcessFlow::SlidingWindowStrategy

Definition of sliging window rate limiting strategy and it’s behaviour implementations. Sliding window allows only N requests for a given key to be done in a trailing O time window where O start is defined as ‘now` - `window_size`. Window expiration starts with the first request for a given key. Once expired it will reset back to 0.

Instance Attribute Summary

Attributes inherited from Strategy

#configuration

Instance Method Summary collapse

Methods inherited from Strategy

execute, #execute, #initialize

Constructor Details

This class inherits a constructor from ExcessFlow::Strategy

Instance Method Details

#within_rate_limit?Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/excess_flow/sliding_window_strategy.rb', line 26

def within_rate_limit?
  ExcessFlow::GlobalMutex.locked(lock_key: configuration.lock_key) do
    cleanup_stale_counters

    if current_requests < configuration.limit
      bump_counter
      start_expiration_window

      true
    else
      false
    end
  end
end