Class: HTTPigeon::CircuitBreaker::FuseConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/httpigeon/circuit_breaker/fuse_config.rb

Constant Summary collapse

DEFAULT_MM_TIMEOUT_HEADER =
'X-Maintenance-Mode-Timeout'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fuse_options = {}) ⇒ FuseConfig

Returns a new instance of FuseConfig.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/httpigeon/circuit_breaker/fuse_config.rb', line 30

def initialize(fuse_options = {})
  @service_id = fuse_options[:service_id].presence || raise(ArgumentError, 'service_id is required')
  @max_failures_count = fuse_options[:max_failures_count] || HTTPigeon.fuse_max_failures_count
  @min_failures_count = fuse_options[:min_failures_count] || HTTPigeon.fuse_min_failures_count
  @failure_rate_threshold = fuse_options[:failure_rate_threshold] || HTTPigeon.fuse_failure_rate_threshold
  @sample_window = fuse_options[:sample_window] || HTTPigeon.fuse_sample_window
  @open_circuit_sleep_window = fuse_options[:open_circuit_sleep_window] || HTTPigeon.fuse_open_circuit_sleep_window
  @error_codes_watchlist = fuse_options[:error_codes_watchlist].to_a | HTTPigeon.fuse_error_codes_watchlist.to_a
  @maintenance_mode_header = fuse_options[:maintenance_mode_header] || DEFAULT_MM_TIMEOUT_HEADER

  @on_circuit_closed = HTTPigeon.fuse_on_circuit_closed
  @on_circuit_opened = HTTPigeon.fuse_on_circuit_opened
  @open_circuit_handler = if HTTPigeon.fuse_open_circuit_handler.respond_to?(:call)
                            HTTPigeon.fuse_open_circuit_handler
                          else
                            ->(api_response, exception) { null_response(api_response, exception) }
                          end
end

Instance Attribute Details

#error_codes_watchlistObject (readonly)

Returns the value of attribute error_codes_watchlist.



18
19
20
# File 'lib/httpigeon/circuit_breaker/fuse_config.rb', line 18

def error_codes_watchlist
  @error_codes_watchlist
end

#failure_rate_thresholdObject (readonly)

Returns the value of attribute failure_rate_threshold.



18
19
20
# File 'lib/httpigeon/circuit_breaker/fuse_config.rb', line 18

def failure_rate_threshold
  @failure_rate_threshold
end

#maintenance_mode_headerObject (readonly)

Returns the value of attribute maintenance_mode_header.



18
19
20
# File 'lib/httpigeon/circuit_breaker/fuse_config.rb', line 18

def maintenance_mode_header
  @maintenance_mode_header
end

#max_failures_countObject (readonly)

Returns the value of attribute max_failures_count.



18
19
20
# File 'lib/httpigeon/circuit_breaker/fuse_config.rb', line 18

def max_failures_count
  @max_failures_count
end

#min_failures_countObject (readonly)

Returns the value of attribute min_failures_count.



18
19
20
# File 'lib/httpigeon/circuit_breaker/fuse_config.rb', line 18

def min_failures_count
  @min_failures_count
end

#on_circuit_closedObject (readonly)

Returns the value of attribute on_circuit_closed.



18
19
20
# File 'lib/httpigeon/circuit_breaker/fuse_config.rb', line 18

def on_circuit_closed
  @on_circuit_closed
end

#on_circuit_openedObject (readonly)

Returns the value of attribute on_circuit_opened.



18
19
20
# File 'lib/httpigeon/circuit_breaker/fuse_config.rb', line 18

def on_circuit_opened
  @on_circuit_opened
end

#open_circuit_handlerObject (readonly)

Returns the value of attribute open_circuit_handler.



18
19
20
# File 'lib/httpigeon/circuit_breaker/fuse_config.rb', line 18

def open_circuit_handler
  @open_circuit_handler
end

#open_circuit_sleep_windowObject (readonly)

Returns the value of attribute open_circuit_sleep_window.



18
19
20
# File 'lib/httpigeon/circuit_breaker/fuse_config.rb', line 18

def open_circuit_sleep_window
  @open_circuit_sleep_window
end

#sample_windowObject (readonly)

Returns the value of attribute sample_window.



18
19
20
# File 'lib/httpigeon/circuit_breaker/fuse_config.rb', line 18

def sample_window
  @sample_window
end

#service_idObject (readonly)

Returns the value of attribute service_id.



18
19
20
# File 'lib/httpigeon/circuit_breaker/fuse_config.rb', line 18

def service_id
  @service_id
end

Instance Method Details

#circuit_open_errorObject



66
67
68
# File 'lib/httpigeon/circuit_breaker/fuse_config.rb', line 66

def circuit_open_error
  CircuitOpenError.new(service_id)
end

#null_response(api_response = nil, exception = nil) ⇒ Object



62
63
64
# File 'lib/httpigeon/circuit_breaker/fuse_config.rb', line 62

def null_response(api_response = nil, exception = nil)
  NullResponse.new(api_response, exception)
end

#to_hObject



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/httpigeon/circuit_breaker/fuse_config.rb', line 49

def to_h
  {
    service_id: service_id,
    max_failures_count: max_failures_count,
    min_failures_count: min_failures_count,
    failure_rate_threshold: failure_rate_threshold,
    sample_window: sample_window,
    open_circuit_sleep_window: open_circuit_sleep_window,
    error_codes_watchlist: error_codes_watchlist,
    maintenance_mode_header: maintenance_mode_header
  }
end