class Thron::CircuitBreaker

Public Class Methods

new(options = {}) click to toggle source
# 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

Public Instance Methods

monitor() { || ... } click to toggle source
# 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?() click to toggle source
# File lib/thron/circuit_breaker.rb, line 29
def open?
  @state == OPEN
end

Private Instance Methods

handle_error(error) click to toggle source
# File lib/thron/circuit_breaker.rb, line 39
def handle_error(error)
  @error_count += 1
  if @error_count >= @threshold
    @state = OPEN
  end
end
handle_success() click to toggle source
# File lib/thron/circuit_breaker.rb, line 35
def handle_success
  @error_count = 0
end