Class: RServiceBus2::CircuitBreaker

Inherits:
Object
  • Object
show all
Defined in:
lib/rservicebus2/circuitbreaker.rb

Overview

An implementation of Michael Nygard’s Circuit Breaker pattern.

Instance Method Summary collapse

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

#brokenObject

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

#failureObject

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
  message_arrived

  ## 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

#liveObject



37
38
39
# File 'lib/rservicebus2/circuitbreaker.rb', line 37

def live
  !broken
end

#resetObject



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

#successObject



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

  message_arrived
end