Class: OnyxCord::Internal::RateLimiter::Gateway
- Inherits:
-
Object
- Object
- OnyxCord::Internal::RateLimiter::Gateway
- Defined in:
- lib/onyxcord/internal/rate_limiter/gateway.rb
Overview
Sliding-window limiter for Gateway sends.
Constant Summary collapse
- DEFAULT_LIMIT =
120- DEFAULT_INTERVAL =
60
Instance Method Summary collapse
-
#initialize(limit: DEFAULT_LIMIT, interval: DEFAULT_INTERVAL, clock: -> { Time.now }, sleeper: ->(duration) { sleep(duration) }) ⇒ Gateway
constructor
A new instance of Gateway.
- #wait ⇒ Object
Constructor Details
#initialize(limit: DEFAULT_LIMIT, interval: DEFAULT_INTERVAL, clock: -> { Time.now }, sleeper: ->(duration) { sleep(duration) }) ⇒ Gateway
Returns a new instance of Gateway.
11 12 13 14 15 16 17 18 |
# File 'lib/onyxcord/internal/rate_limiter/gateway.rb', line 11 def initialize(limit: DEFAULT_LIMIT, interval: DEFAULT_INTERVAL, clock: -> { Time.now }, sleeper: ->(duration) { sleep(duration) }) @limit = limit @interval = interval @clock = clock @sleeper = sleeper @sent_at = [] @mutex = Mutex.new end |
Instance Method Details
#wait ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/onyxcord/internal/rate_limiter/gateway.rb', line 20 def wait @mutex.synchronize do now = @clock.call prune(now) if @sent_at.length >= @limit sleep_for = @interval - (now - @sent_at.first) @sleeper.call(sleep_for) if sleep_for.positive? now = @clock.call prune(now) end @sent_at << now end end |