Class: RServiceBus2::CircuitBreaker
- Inherits:
-
Object
- Object
- RServiceBus2::CircuitBreaker
- Defined in:
- lib/rservicebus2/circuitbreaker.rb
Overview
An implementation of Michael Nygard’s Circuit Breaker pattern.
Instance Method Summary collapse
-
#broken ⇒ Object
Public Interface Broken will be called before processing a message.
-
#failure ⇒ Object
This should be called less than success.
-
#initialize(host) ⇒ CircuitBreaker
constructor
A new instance of CircuitBreaker.
- #live ⇒ Object
- #reset ⇒ Object
- #success ⇒ Object
Constructor Details
#initialize(host) ⇒ CircuitBreaker
Returns a new instance of CircuitBreaker.
19 20 21 22 23 24 25 26 27 |
# File 'lib/rservicebus2/circuitbreaker.rb', line 19 def initialize(host) @host = host @maxnumber_of_failures = RServiceBus2.get_value('RSBCB_MAX', 5) @seconds_to_break = RServiceBus2.get_value('RSBCB_SECONDS_TO_BREAK', 60).to_i @seconds_to_reset = RServiceBus2.get_value('RSBCB_SECONDS_TO_RESET', 60).to_i @reset_on_success = RServiceBus2.get_value('RSBCB_RESET_ON_SUCCESS', false) reset end |
Instance Method Details
#broken ⇒ Object
Public Interface Broken will be called before processing a message.
=> Broken will be called before Failure
32 33 34 35 |
# File 'lib/rservicebus2/circuitbreaker.rb', line 32 def broken reset if !@time_to_reset.nil? && Time.now > @time_to_reset @broken end |
#failure ⇒ Object
This should be called less than success. If there is a failure, then taking a bit longer gives time to settle.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/rservicebus2/circuitbreaker.rb', line 43 def failure ## logFirstFailure if @number_of_failures.zero? @number_of_failures = 1 @time_of_first_failure = Time.now @time_to_break = @time_of_first_failure + @seconds_to_break else @number_of_failures += 1 end ## checkToBreakCircuit break_circuit if @number_of_failures >= @maxnumber_of_failures end |
#live ⇒ Object
37 38 39 |
# File 'lib/rservicebus2/circuitbreaker.rb', line 37 def live !broken end |
#reset ⇒ Object
9 10 11 12 13 14 15 16 17 |
# File 'lib/rservicebus2/circuitbreaker.rb', line 9 def reset @broken = false @number_of_failures = 0 @time_of_first_failure = nil @time_to_break = nil @time_to_reset = nil end |
#success ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/rservicebus2/circuitbreaker.rb', line 59 def success if @reset_on_success == true reset return end end |