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) ⇒ 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
# 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)

  @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

  @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:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/semian/circuit_breaker.rb', line 38

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



88
89
90
91
92
# File 'lib/semian/circuit_breaker.rb', line 88

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

#in_use?Boolean

Returns:

  • (Boolean)


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

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

#mark_failed(error) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/semian/circuit_breaker.rb', line 65

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

#mark_successObject



75
76
77
78
79
80
# File 'lib/semian/circuit_breaker.rb', line 75

def mark_success
  return unless half_open?

  @successes.increment
  transition_to_close if success_threshold_reached?
end

#request_allowed?Boolean

Returns:

  • (Boolean)


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

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

#resetObject



82
83
84
85
86
# File 'lib/semian/circuit_breaker.rb', line 82

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

#transition_to_half_open?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/semian/circuit_breaker.rb', line 57

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