Class: Semian::CircuitBreaker
- Inherits:
-
Object
- Object
- Semian::CircuitBreaker
- Extended by:
- Forwardable
- Defined in:
- lib/semian/circuit_breaker.rb
Overview
:nodoc:
Instance Method Summary collapse
- #acquire ⇒ Object
- #destroy ⇒ Object
-
#initialize(name, exceptions:, success_threshold:, error_threshold:, error_timeout:, implementation:) ⇒ CircuitBreaker
constructor
A new instance of CircuitBreaker.
- #mark_failed(_error) ⇒ Object
- #mark_success ⇒ Object
- #request_allowed? ⇒ Boolean
- #reset ⇒ Object
Constructor Details
#initialize(name, exceptions:, success_threshold:, error_threshold:, error_timeout:, implementation:) ⇒ CircuitBreaker
Returns a new instance of CircuitBreaker.
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/semian/circuit_breaker.rb', line 5 def initialize(name, exceptions:, success_threshold:, error_threshold:, error_timeout:, implementation:) @name = name.to_sym @success_count_threshold = success_threshold @error_count_threshold = error_threshold @error_timeout = error_timeout @exceptions = exceptions @errors = implementation::SlidingWindow.new(max_size: @error_count_threshold) @successes = implementation::Integer.new @state = implementation::State.new end |
Instance Method Details
#acquire ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/semian/circuit_breaker.rb', line 17 def acquire raise OpenCircuitError unless request_allowed? result = nil begin result = yield rescue *@exceptions => error mark_failed(error) raise error else mark_success end result end |
#destroy ⇒ Object
59 60 61 62 63 |
# File 'lib/semian/circuit_breaker.rb', line 59 def destroy @errors.destroy @successes.destroy @state.destroy end |
#mark_failed(_error) ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/semian/circuit_breaker.rb', line 38 def mark_failed(_error) push_time(@errors, duration: @error_timeout) if closed? open if error_threshold_reached? elsif half_open? open end end |
#mark_success ⇒ Object
47 48 49 50 51 |
# File 'lib/semian/circuit_breaker.rb', line 47 def mark_success return unless half_open? @successes.increment close if success_threshold_reached? end |
#request_allowed? ⇒ Boolean
32 33 34 35 36 |
# File 'lib/semian/circuit_breaker.rb', line 32 def request_allowed? return true if closed? half_open if error_timeout_expired? !open? end |
#reset ⇒ Object
53 54 55 56 57 |
# File 'lib/semian/circuit_breaker.rb', line 53 def reset @errors.clear @successes.reset close end |