Class: Thron::CircuitBreaker
- Inherits:
-
Object
- Object
- Thron::CircuitBreaker
- Defined in:
- lib/thron/circuit_breaker.rb
Defined Under Namespace
Classes: OpenError
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ CircuitBreaker
constructor
A new instance of CircuitBreaker.
- #monitor ⇒ Object
- #open? ⇒ Boolean
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( = {}) @state = CLOSED @threshold = .fetch(:threshold) { 5 } @error_count = 0 @ignored = [:ignored].to_a end |
Instance Method Details
#monitor ⇒ Object
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
29 30 31 |
# File 'lib/thron/circuit_breaker.rb', line 29 def open? @state == OPEN end |