Class: Aikido::Zen::RateLimiter::Breaker Private

Inherits:
Object
  • Object
show all
Defined in:
lib/aikido/zen/rate_limiter/breaker.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Circuit breaker that rate limits internal API requests in two ways: By using a sliding window, to allow only a certain number of events over that window, and with the ability of manually being tripped open when the API responds to a request with a 429.

Instance Method Summary collapse

Constructor Details

#initialize(config: Aikido::Zen.config, clock: RateLimiter::Bucket::DEFAULT_CLOCK) ⇒ Breaker

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Breaker.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/aikido/zen/rate_limiter/breaker.rb', line 13

def initialize(config: Aikido::Zen.config, clock: RateLimiter::Bucket::DEFAULT_CLOCK)
  @config = config
  @clock = clock

  @bucket = RateLimiter::Bucket.new(
    ttl: config.client_rate_limit_period,
    max_size: config.client_rate_limit_max_events,
    clock: clock
  )
  @opened_at = nil
end

Instance Method Details

#open!void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Trip the circuit open to force all events to be throttled until the deadline passes.



30
31
32
# File 'lib/aikido/zen/rate_limiter/breaker.rb', line 30

def open!
  @opened_at = @clock.call
end

#throttle?(event) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • event (#type)

    an event which we’ll discriminate by type to decide if we should throttle it.

Returns:

  • (Boolean)


37
38
39
40
41
42
# File 'lib/aikido/zen/rate_limiter/breaker.rb', line 37

def throttle?(event)
  return true if open? && !try_close

  result = @bucket.increment(event.type)
  result.throttled?
end