Class: Supervision::CircuitBreaker
- Inherits:
-
Object
- Object
- Supervision::CircuitBreaker
- Extended by:
- Forwardable
- Includes:
- Timeout
- Defined in:
- lib/supervision/circuit_breaker.rb
Overview
A class responsible for protecting remote calls
Instance Attribute Summary collapse
-
#control ⇒ Object
readonly
Returns the value of attribute control.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#before(&block) ⇒ Object
Define before handler.
-
#call(*args) ⇒ Object
Executes the dangerous call.
-
#configure {|Configuration| ... } ⇒ Object
Configure circuit instance parameters.
-
#initialize(options = {}, &block) ⇒ CircuitBreaker
constructor
Create a CircuitBreaker.
-
#inspect ⇒ String
Detailed string representation of this circuit.
-
#on_failure(&block) ⇒ Supervision::CircuitBreaker
(also: #on_open)
Define failure handler.
-
#on_success(&block) ⇒ Supervision::CircuitBreaker
(also: #on_closed)
Define success handler.
-
#reset! ⇒ nil
Reset this circuit to closed state.
-
#to_s ⇒ String
Detailed string representation of this circuit.
Constructor Details
#initialize(options = {}, &block) ⇒ CircuitBreaker
Create a CircuitBreaker
22 23 24 25 26 27 28 29 30 |
# File 'lib/supervision/circuit_breaker.rb', line 22 def initialize( = {}, &block) if block.nil? raise InvalidParameterError, 'CircuitBreaker.new requires a block' end @name = .delete(:name) @control = CircuitControl.new() @circuit = Atomic.new(block) @mutex = Mutex.new end |
Instance Attribute Details
#control ⇒ Object (readonly)
Returns the value of attribute control.
9 10 11 |
# File 'lib/supervision/circuit_breaker.rb', line 9 def control @control end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
11 12 13 |
# File 'lib/supervision/circuit_breaker.rb', line 11 def name @name end |
Instance Method Details
#before(&block) ⇒ Object
Define before handler
73 74 75 76 |
# File 'lib/supervision/circuit_breaker.rb', line 73 def before(&block) @before = block self end |
#call(*args) ⇒ Object
Executes the dangerous call
# TODO: this should distribute calls so we don’t wait
in sync call for timeout
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/supervision/circuit_breaker.rb', line 47 def call(*args) handle_before begin result = dispatch(*args) handle_success result rescue Exception => error handle_failure(error) end end |
#configure {|Configuration| ... } ⇒ Object
Configure circuit instance parameters
37 38 39 |
# File 'lib/supervision/circuit_breaker.rb', line 37 def configure(&block) control.config.configure(&block) end |
#inspect ⇒ String
Detailed string representation of this circuit
105 106 107 |
# File 'lib/supervision/circuit_breaker.rb', line 105 def inspect "#<#{self.class.name}:#{object_id} @name=#{name}>" end |
#on_failure(&block) ⇒ Supervision::CircuitBreaker Also known as: on_open
Define failure handler
94 95 96 97 |
# File 'lib/supervision/circuit_breaker.rb', line 94 def on_failure(&block) @on_failure = block self end |
#on_success(&block) ⇒ Supervision::CircuitBreaker Also known as: on_closed
Define success handler
83 84 85 86 |
# File 'lib/supervision/circuit_breaker.rb', line 83 def on_success(&block) @on_success = block self end |
#reset! ⇒ nil
Reset this circuit to closed state
66 67 68 |
# File 'lib/supervision/circuit_breaker.rb', line 66 def reset! control.reset! end |
#to_s ⇒ String
Detailed string representation of this circuit
114 115 116 |
# File 'lib/supervision/circuit_breaker.rb', line 114 def to_s "#<#{self.class.name}:#{object_id} @name=#{name}>" end |