Class: RServiceBus::CircuitBreaker
- Inherits:
-
Object
- Object
- RServiceBus::CircuitBreaker
- Defined in:
- lib/rservicebus/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.
22 23 24 25 26 27 28 29 30 |
# File 'lib/rservicebus/CircuitBreaker.rb', line 22 def initialize( host ) @host = host @maxNumberOfFailures = RServiceBus.getValue( 'RSBCB_MAX', 5 ) @secondsToBreak = RServiceBus.getValue( 'RSBCB_SECONDS_TO_BREAK', 60 ).to_i @secondsToReset = RServiceBus.getValue( 'RSBCB_SECONDS_TO_RESET', 60 ).to_i @resetOnSuccess = RServiceBus.getValue( 'RSBCB_RESET_ON_SUCCESS', false ) self.reset end |
Instance Method Details
#Broken ⇒ Object
Public Interface Broken will be called before processing a message.
=> Broken will be called before Failure
35 36 37 38 39 40 41 |
# File 'lib/rservicebus/CircuitBreaker.rb', line 35 def Broken if !@timeToReset.nil? && Time.now > @timeToReset then self.reset end return @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.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/rservicebus/CircuitBreaker.rb', line 50 def Failure self. ##logFirstFailure if @numberOfFailures == 0 @numberOfFailures = 1 @timeOfFirstFailure = Time.now @timeToBreak = @timeOfFirstFailure + @secondsToBreak else @numberOfFailures = @numberOfFailures + 1 end ##checkToBreakCircuit if @numberOfFailures >= @maxNumberOfFailures then self.breakCircuit end end |
#Live ⇒ Object
43 44 45 |
# File 'lib/rservicebus/CircuitBreaker.rb', line 43 def Live return !self.Broken end |
#reset ⇒ Object
12 13 14 15 16 17 18 19 20 |
# File 'lib/rservicebus/CircuitBreaker.rb', line 12 def reset @broken = false @numberOfFailures = 0 @timeOfFirstFailure = nil @timeToBreak = nil @timeToReset = nil end |
#Success ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/rservicebus/CircuitBreaker.rb', line 70 def Success if @resetOnSuccess == true then self.reset return end self. end |