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.



17
18
19
20
21
22
23
24
25
# File 'lib/rservicebus2/circuitbreaker.rb', line 17

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


30
31
32
33
# File 'lib/rservicebus2/circuitbreaker.rb', line 30

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.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rservicebus2/circuitbreaker.rb', line 41

def failure
  message_arrived

  ## logFirstFailure
  if @number_of_failures == 0
    @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



35
36
37
# File 'lib/rservicebus2/circuitbreaker.rb', line 35

def live
  !broken
end

#resetObject



7
8
9
10
11
12
13
14
15
# File 'lib/rservicebus2/circuitbreaker.rb', line 7

def reset
  @broken = false

  @number_of_failures = 0
  @time_of_first_failure = nil

  @time_to_break = nil
  @time_to_reset = nil
end

#successObject



57
58
59
60
61
62
63
64
# File 'lib/rservicebus2/circuitbreaker.rb', line 57

def success
  if @reset_on_success == true
    reset
    return
  end

  message_arrived
end