Class: Philiprehberger::RateLimiter::SlidingWindow
- Inherits:
-
Object
- Object
- Philiprehberger::RateLimiter::SlidingWindow
- Includes:
- StatsTracking
- Defined in:
- lib/philiprehberger/rate_limiter/sliding_window.rb
Instance Attribute Summary collapse
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#window ⇒ Object
readonly
Returns the value of attribute window.
Instance Method Summary collapse
-
#allow?(key, weight: 1) ⇒ Boolean
Check if a request is allowed and consume slot(s) in the window.
-
#allow_batch(keys) ⇒ Hash{Object => Boolean}
Check many keys in a single mutex acquisition.
-
#clear ⇒ void
Clear state for all keys (resets quotas and stats for every tracked key).
-
#drain(key = :default) ⇒ Integer
Forcefully consume all remaining capacity for a key.
-
#info(key) ⇒ Hash
Build a usage info hash for a key.
-
#info_batch(keys) ⇒ Hash{Object => Hash}
Build info hashes for many keys in a single mutex acquisition.
-
#initialize(limit:, window:) ⇒ SlidingWindow
constructor
A new instance of SlidingWindow.
-
#peek(key) ⇒ Boolean
Check if a request would be allowed without consuming a slot.
-
#refund(key, amount: 1) ⇒ nil
Return previously consumed slot(s) to a key (e.g. on downstream failure).
-
#remaining(key) ⇒ Integer
Return the number of remaining slots in the current window.
-
#reset(key) ⇒ void
Clear the window for a key, discarding all tracked entries.
-
#retry_after(key = :default) ⇒ Float
Seconds until the next request would be allowed, suitable for the HTTP Retry-After header.
-
#used(key) ⇒ Integer
Number of currently consumed slots for a key (after expiring old entries).
-
#wait_time(key = :default) ⇒ Float
Seconds until the next request would be allowed.
-
#window_reset_at(key = :default) ⇒ Time?
Time when the current window expires.
Methods included from StatsTracking
#allow!, #keys, #on_reject, #stats, #throttle
Constructor Details
#initialize(limit:, window:) ⇒ SlidingWindow
Returns a new instance of SlidingWindow.
12 13 14 15 16 17 18 |
# File 'lib/philiprehberger/rate_limiter/sliding_window.rb', line 12 def initialize(limit:, window:) @limit = limit @window = window @store = {} @mutex = Mutex.new init_stats end |
Instance Attribute Details
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
10 11 12 |
# File 'lib/philiprehberger/rate_limiter/sliding_window.rb', line 10 def limit @limit end |
#window ⇒ Object (readonly)
Returns the value of attribute window.
10 11 12 |
# File 'lib/philiprehberger/rate_limiter/sliding_window.rb', line 10 def window @window end |
Instance Method Details
#allow?(key, weight: 1) ⇒ Boolean
Check if a request is allowed and consume slot(s) in the window.
25 26 27 |
# File 'lib/philiprehberger/rate_limiter/sliding_window.rb', line 25 def allow?(key, weight: 1) @mutex.synchronize { try_acquire(key, weight) } end |
#allow_batch(keys) ⇒ Hash{Object => Boolean}
Check many keys in a single mutex acquisition.
33 34 35 36 37 |
# File 'lib/philiprehberger/rate_limiter/sliding_window.rb', line 33 def allow_batch(keys) @mutex.synchronize do keys.to_h { |key| [key, try_acquire(key, 1)] } end end |
#clear ⇒ void
This method returns an undefined value.
Clear state for all keys (resets quotas and stats for every tracked key)
77 78 79 80 81 82 83 |
# File 'lib/philiprehberger/rate_limiter/sliding_window.rb', line 77 def clear @mutex.synchronize do @store.clear @stats_store.clear end nil end |
#drain(key = :default) ⇒ Integer
Forcefully consume all remaining capacity for a key.
118 119 120 |
# File 'lib/philiprehberger/rate_limiter/sliding_window.rb', line 118 def drain(key = :default) @mutex.synchronize { drain_entries(key) } end |
#info(key) ⇒ Hash
Build a usage info hash for a key.
89 90 91 |
# File 'lib/philiprehberger/rate_limiter/sliding_window.rb', line 89 def info(key) @mutex.synchronize { build_info(key) } end |
#info_batch(keys) ⇒ Hash{Object => Hash}
Build info hashes for many keys in a single mutex acquisition.
Mirrors #allow_batch for inspection rather than acquisition.
99 100 101 102 103 |
# File 'lib/philiprehberger/rate_limiter/sliding_window.rb', line 99 def info_batch(keys) @mutex.synchronize do keys.to_h { |key| [key, build_info(key)] } end end |
#peek(key) ⇒ Boolean
Check if a request would be allowed without consuming a slot.
43 44 45 |
# File 'lib/philiprehberger/rate_limiter/sliding_window.rb', line 43 def peek(key) @mutex.synchronize { count_remaining(key).positive? } end |
#refund(key, amount: 1) ⇒ nil
Return previously consumed slot(s) to a key (e.g. on downstream failure).
110 111 112 |
# File 'lib/philiprehberger/rate_limiter/sliding_window.rb', line 110 def refund(key, amount: 1) @mutex.synchronize { refund_entries(key, amount) } end |
#remaining(key) ⇒ Integer
Return the number of remaining slots in the current window.
51 52 53 |
# File 'lib/philiprehberger/rate_limiter/sliding_window.rb', line 51 def remaining(key) @mutex.synchronize { count_remaining(key) } end |
#reset(key) ⇒ void
This method returns an undefined value.
Clear the window for a key, discarding all tracked entries.
70 71 72 |
# File 'lib/philiprehberger/rate_limiter/sliding_window.rb', line 70 def reset(key) @mutex.synchronize { @store.delete(key.to_s) } end |
#retry_after(key = :default) ⇒ Float
Seconds until the next request would be allowed, suitable for the HTTP Retry-After header. Returns 0.0 when a request is allowed right now.
145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/philiprehberger/rate_limiter/sliding_window.rb', line 145 def retry_after(key = :default) @mutex.synchronize do cleanup(key) entries = fetch_entries(key) return 0.0 if entries.length < @limit oldest = entries.min return 0.0 if oldest.nil? wait = (oldest + @window) - now [wait, 0.0].max end end |
#used(key) ⇒ Integer
Number of currently consumed slots for a key (after expiring old entries).
59 60 61 62 63 64 |
# File 'lib/philiprehberger/rate_limiter/sliding_window.rb', line 59 def used(key) @mutex.synchronize do cleanup(key) fetch_entries(key).length end end |
#wait_time(key = :default) ⇒ Float
Seconds until the next request would be allowed
126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/philiprehberger/rate_limiter/sliding_window.rb', line 126 def wait_time(key = :default) @mutex.synchronize do cleanup(key) entries = fetch_entries(key) return 0.0 if entries.length < @limit oldest = entries.min return 0.0 if oldest.nil? wait = oldest + @window - now [wait, 0.0].max end end |
#window_reset_at(key = :default) ⇒ Time?
Time when the current window expires
163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/philiprehberger/rate_limiter/sliding_window.rb', line 163 def window_reset_at(key = :default) @mutex.synchronize do entries = fetch_entries(key) return nil if entries.empty? cleanup(key) entries = fetch_entries(key) return nil if entries.empty? oldest = entries.min elapsed = now - oldest Time.now + (@window - elapsed) end end |