Class: Thron::CircuitBreaker

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

Defined Under Namespace

Classes: OpenError

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CircuitBreaker

Returns a new instance of CircuitBreaker.



9
10
11
12
13
14
# File 'lib/thron/circuit_breaker.rb', line 9

def initialize(options = {})
  @state = CLOSED
  @threshold = options.fetch(:threshold) { 5 }
  @error_count = 0
  @ignored = options[:ignored].to_a
end

Instance Method Details

#monitorObject



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/thron/circuit_breaker.rb', line 16

def monitor
  return yield if @threshold.zero?
  fail OpenError, 'the circuit breaker is open!' if open?
  result = yield
  handle_success
  result
rescue OpenError
  raise
rescue => error
  handle_error(error) unless @ignored.include?(error.class)
  raise
end

#open?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/thron/circuit_breaker.rb', line 29

def open?
  @state == OPEN
end