Class: Philiprehberger::RateLimiter::SlidingWindow

Inherits:
Object
  • Object
show all
Includes:
StatsTracking
Defined in:
lib/philiprehberger/rate_limiter/sliding_window.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from StatsTracking

#allow!, #block, #keys, #on_reject, #stats, #throttle

Constructor Details

#initialize(limit:, window:, max_keys: nil) ⇒ SlidingWindow

Returns a new instance of SlidingWindow.

Parameters:

  • limit (Integer)

    max requests allowed per window

  • window (Numeric)

    window duration in seconds

  • max_keys (Integer, nil) (defaults to: nil)

    cap on tracked keys; least-recently-touched key is evicted (LRU) once the cap is exceeded. nil means unbounded.



16
17
18
19
20
21
22
23
# File 'lib/philiprehberger/rate_limiter/sliding_window.rb', line 16

def initialize(limit:, window:, max_keys: nil)
  @limit = limit
  @window = window
  @max_keys = max_keys
  @store = {}
  @mutex = Mutex.new
  init_stats
end

Instance Attribute Details

#limitObject (readonly)

Returns the value of attribute limit.



10
11
12
# File 'lib/philiprehberger/rate_limiter/sliding_window.rb', line 10

def limit
  @limit
end

#windowObject (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.

Parameters:

  • key (Symbol, String)

    the rate limit key

  • weight (Integer) (defaults to: 1)

    number of slots to consume (default 1)

Returns:

  • (Boolean)

    true if the request was allowed, false if rejected



30
31
32
# File 'lib/philiprehberger/rate_limiter/sliding_window.rb', line 30

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.

Parameters:

  • keys (Array<Symbol, String>)

    the keys to check and consume

Returns:

  • (Hash{Object => Boolean})

    mapping of each key to the allow result



38
39
40
41
42
# File 'lib/philiprehberger/rate_limiter/sliding_window.rb', line 38

def allow_batch(keys)
  @mutex.synchronize do
    keys.to_h { |key| [key, try_acquire(key, 1)] }
  end
end

#clearvoid

This method returns an undefined value.

Clear state for all keys (resets quotas and stats for every tracked key)



82
83
84
85
86
87
88
# File 'lib/philiprehberger/rate_limiter/sliding_window.rb', line 82

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.

Parameters:

  • key (Symbol, String) (defaults to: :default)

    the rate limit key

Returns:

  • (Integer)

    the number of slots drained



143
144
145
# File 'lib/philiprehberger/rate_limiter/sliding_window.rb', line 143

def drain(key = :default)
  @mutex.synchronize { drain_entries(key) }
end

#info(key) ⇒ Hash

Build a usage info hash for a key.

Parameters:

  • key (Symbol, String)

    the rate limit key

Returns:

  • (Hash)

    keys :remaining, :reset_at, :limit, :window, :used



94
95
96
# File 'lib/philiprehberger/rate_limiter/sliding_window.rb', line 94

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.

Parameters:

  • keys (Array<Symbol, String>)

    the keys to inspect

Returns:

  • (Hash{Object => Hash})

    mapping of each key to its info hash



104
105
106
107
108
# File 'lib/philiprehberger/rate_limiter/sliding_window.rb', line 104

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.

Parameters:

  • key (Symbol, String)

    the rate limit key

Returns:

  • (Boolean)

    true if capacity is available, false otherwise



48
49
50
# File 'lib/philiprehberger/rate_limiter/sliding_window.rb', line 48

def peek(key)
  @mutex.synchronize { count_remaining(key).positive? }
end

#pruneInteger

Drop keys whose window is empty (all entries have expired), reclaiming memory for idle keys. Also clears their stats.

Returns:

  • (Integer)

    the number of keys pruned



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/philiprehberger/rate_limiter/sliding_window.rb', line 123

def prune
  @mutex.synchronize do
    pruned = 0
    tracked = @store.keys
    tracked.each do |key|
      cleanup(key)
      next unless @store[key].empty?

      @store.delete(key)
      @stats_store.delete(key)
      pruned += 1
    end
    pruned
  end
end

#refund(key, amount: 1) ⇒ nil

Return previously consumed slot(s) to a key (e.g. on downstream failure).

Parameters:

  • key (Symbol, String)

    the rate limit key

  • amount (Integer) (defaults to: 1)

    number of slots to refund (default 1)

Returns:

  • (nil)


115
116
117
# File 'lib/philiprehberger/rate_limiter/sliding_window.rb', line 115

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.

Parameters:

  • key (Symbol, String)

    the rate limit key

Returns:

  • (Integer)

    count of remaining capacity (0 if at or over limit)



56
57
58
# File 'lib/philiprehberger/rate_limiter/sliding_window.rb', line 56

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.

Parameters:

  • key (Symbol, String)

    the rate limit key



75
76
77
# File 'lib/philiprehberger/rate_limiter/sliding_window.rb', line 75

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.

Parameters:

  • key (Symbol, String) (defaults to: :default)

    the rate limit key

Returns:

  • (Float)

    seconds until next allowed request (0.0 if allowed now)



170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/philiprehberger/rate_limiter/sliding_window.rb', line 170

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).

Parameters:

  • key (Symbol, String)

    the rate limit key

Returns:

  • (Integer)

    count of active entries in the window



64
65
66
67
68
69
# File 'lib/philiprehberger/rate_limiter/sliding_window.rb', line 64

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

Parameters:

  • key (Symbol, String) (defaults to: :default)

    the rate limit key

Returns:

  • (Float)

    seconds to wait (0 if allowed now)



151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/philiprehberger/rate_limiter/sliding_window.rb', line 151

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

Parameters:

  • key (Symbol, String) (defaults to: :default)

    the rate limit key

Returns:

  • (Time, nil)

    absolute time when window resets, nil if no requests



188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/philiprehberger/rate_limiter/sliding_window.rb', line 188

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