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!, #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

#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

Returns:

  • (Boolean)


20
21
22
# File 'lib/philiprehberger/rate_limiter/sliding_window.rb', line 20

def allow?(key, weight: 1)
  @mutex.synchronize { try_acquire(key, weight) }
end

#clearvoid

This method returns an undefined value.

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



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

def clear
  @mutex.synchronize do
    @store.clear
    @stats_store.clear
  end
  nil
end

#info(key) ⇒ Object



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

def info(key)
  @mutex.synchronize { build_info(key) }
end

#peek(key) ⇒ Object



24
25
26
# File 'lib/philiprehberger/rate_limiter/sliding_window.rb', line 24

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

#refund(key, amount: 1) ⇒ Object



51
52
53
# File 'lib/philiprehberger/rate_limiter/sliding_window.rb', line 51

def refund(key, amount: 1)
  @mutex.synchronize { refund_entries(key, amount) }
end

#remaining(key) ⇒ Object



28
29
30
# File 'lib/philiprehberger/rate_limiter/sliding_window.rb', line 28

def remaining(key)
  @mutex.synchronize { count_remaining(key) }
end

#reset(key) ⇒ Object



32
33
34
# File 'lib/philiprehberger/rate_limiter/sliding_window.rb', line 32

def reset(key)
  @mutex.synchronize { @store.delete(key.to_s) }
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)



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/philiprehberger/rate_limiter/sliding_window.rb', line 59

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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/philiprehberger/rate_limiter/sliding_window.rb', line 77

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