Class: AllMyCircuits::Strategies::PercentageOverWindowStrategy

Inherits:
AbstractWindowStrategy show all
Defined in:
lib/all_my_circuits/strategies/percentage_over_window_strategy.rb

Overview

Public: opens the circuit whenever failures threshold is reached within the window. Threshold is represented by a percentage of failures within the window.

Instance Method Summary collapse

Methods inherited from AbstractWindowStrategy

#closed, #error, #opened, #success

Methods inherited from AbstractStrategy

#closed, #error, #opened, #success

Constructor Details

#initialize(failure_rate_percent_threshold:, **kwargs) ⇒ PercentageOverWindowStrategy

Public: initializes a new instance.

Options

requests_window                - number of consecutive requests tracked by the window.
failure_rate_percent_threshold - percent rate of failures within the window after which
                                 the circuit is tripped open.


18
19
20
21
# File 'lib/all_my_circuits/strategies/percentage_over_window_strategy.rb', line 18

def initialize(failure_rate_percent_threshold:, **kwargs)
  @failure_rate_percent_threshold = failure_rate_percent_threshold
  super(**kwargs)
end

Instance Method Details

#should_open?Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
# File 'lib/all_my_circuits/strategies/percentage_over_window_strategy.rb', line 23

def should_open?
  return false unless @window.full?

  failure_rate_percent = ((@window.count(:failed).to_f / @window.count) * 100).ceil
  failure_rate_percent >= @failure_rate_percent_threshold
end