Class: Semian::CircuitBreaker

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/semian/circuit_breaker.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, exceptions:, success_threshold:, error_threshold:, error_timeout:, implementation:, half_open_resource_timeout: nil, error_threshold_timeout: nil, error_threshold_timeout_enabled: true, lumping_interval: 0) ⇒ CircuitBreaker

Returns a new instance of CircuitBreaker.


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/semian/circuit_breaker.rb', line 18

def initialize(name, exceptions:, success_threshold:, error_threshold:,
  error_timeout:, implementation:, half_open_resource_timeout: nil,
  error_threshold_timeout: nil, error_threshold_timeout_enabled: true,
  lumping_interval: 0)
  @name = name.to_sym
  @success_count_threshold = success_threshold
  @error_count_threshold = error_threshold
  @error_threshold_timeout = error_threshold_timeout || error_timeout
  @error_threshold_timeout_enabled = error_threshold_timeout_enabled.nil? ? true : error_threshold_timeout_enabled
  @error_timeout = error_timeout
  @exceptions = exceptions
  @half_open_resource_timeout = half_open_resource_timeout
  @lumping_interval = lumping_interval

  if @lumping_interval > @error_threshold_timeout
    raise ArgumentError, "lumping_interval (#{@lumping_interval}) must be less than error_threshold_timeout (#{@error_threshold_timeout})"
  end

  @errors = implementation::SlidingWindow.new(max_size: @error_count_threshold)
  @successes = implementation::Integer.new
  @state = implementation::State.new

  reset
end

Instance Attribute Details

#error_timeoutObject (readonly)

Returns the value of attribute error_timeout.


9
10
11
# File 'lib/semian/circuit_breaker.rb', line 9

def error_timeout
  @error_timeout
end

#half_open_resource_timeoutObject (readonly)

Returns the value of attribute half_open_resource_timeout.


9
10
11
# File 'lib/semian/circuit_breaker.rb', line 9

def half_open_resource_timeout
  @half_open_resource_timeout
end

#last_errorObject (readonly)

Returns the value of attribute last_error.


9
10
11
# File 'lib/semian/circuit_breaker.rb', line 9

def last_error
  @last_error
end

#nameObject (readonly)

Returns the value of attribute name.


9
10
11
# File 'lib/semian/circuit_breaker.rb', line 9

def name
  @name
end

#stateObject (readonly)

Returns the value of attribute state.


9
10
11
# File 'lib/semian/circuit_breaker.rb', line 9

def state
  @state
end

Instance Method Details

#acquire(resource = nil, &block) ⇒ Object

Raises:


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/semian/circuit_breaker.rb', line 43

def acquire(resource = nil, &block)
  transition_to_half_open if transition_to_half_open?

  raise OpenCircuitError unless request_allowed?

  result = nil
  begin
    result = maybe_with_half_open_resource_timeout(resource, &block)
  rescue *@exceptions => error
    if !error.respond_to?(:marks_semian_circuits?) || error.marks_semian_circuits?
      mark_failed(error)
    end
    raise error
  else
    mark_success
  end
  result
end

#destroyObject


92
93
94
95
96
# File 'lib/semian/circuit_breaker.rb', line 92

def destroy
  @errors.destroy
  @successes.destroy
  @state.destroy
end

#in_use?Boolean

Returns:

  • (Boolean)

98
99
100
# File 'lib/semian/circuit_breaker.rb', line 98

def in_use?
  !error_timeout_expired? && !@errors.empty?
end

#mark_failed(error) ⇒ Object


70
71
72
73
74
75
76
77
# File 'lib/semian/circuit_breaker.rb', line 70

def mark_failed(error)
  push_error(error)
  if closed?
    transition_to_open if error_threshold_reached?
  elsif half_open?
    transition_to_open
  end
end

#mark_successObject


79
80
81
82
83
84
# File 'lib/semian/circuit_breaker.rb', line 79

def mark_success
  return unless half_open?

  @successes.increment
  transition_to_close if success_threshold_reached?
end

#request_allowed?Boolean

Returns:

  • (Boolean)

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

def request_allowed?
  closed? || half_open? || transition_to_half_open?
end

#resetObject


86
87
88
89
90
# File 'lib/semian/circuit_breaker.rb', line 86

def reset
  @errors.clear
  @successes.reset
  transition_to_close
end

#transition_to_half_open?Boolean

Returns:

  • (Boolean)

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

def transition_to_half_open?
  open? && error_timeout_expired? && !half_open?
end