Module: CircuitBreaker

Defined in:
lib/circuit_breaker.rb

Overview

CircuitBreaker is a relatively simple Ruby mixin that will wrap a call to a given service in a circuit breaker pattern.

The circuit starts off “closed” meaning that all calls will go through. However, consecutive failures are recorded and after a threshold is reached, the circuit will “trip”, setting the circuit into an “open” state.

In an “open” state, every call to the service will fail by raising CircuitBrokenException.

The circuit will remain in an “open” state until the failure timeout has elapsed.

After the failure_timeout has elapsed, the circuit will go into a “half open” state and the call will go through. A failure will immediately pop the circuit open again, and a success will close the circuit and reset the failure count.

require ‘circuit_breaker’ class TestService

include CircuitBreaker

def call_remote_service() ...

circuit_method :call_remote_service

# Optional
circuit_handler do |handler|
  handler.logger = Logger.new(STDOUT)
  handler.failure_threshold = 5
  handler.failure_timeout = 5
end

# Optional
circuit_handler_class MyCustomCircuitHandler

end

Copyright 2009 Will Sargent Author: Will Sargent <[email protected]> Many thanks to Devin Mullins

Defined Under Namespace

Modules: ClassMethods Classes: CircuitBrokenException, CircuitHandler, CircuitState

Constant Summary collapse

VERSION =
'1.0.0'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object

Extends the included class with CircuitBreaker



51
52
53
# File 'lib/circuit_breaker.rb', line 51

def self.included(klass)
  klass.extend ::CircuitBreaker::ClassMethods
end

Instance Method Details

#circuit_stateObject

Returns the current circuit state. This is defined on the instance, so you can have several instances of the same class with different states.



59
60
61
# File 'lib/circuit_breaker.rb', line 59

def circuit_state
  @circuit_state ||= self.class.circuit_handler.new_circuit_state
end