Class: CircuitBreakage::Breaker

Inherits:
Object
  • Object
show all
Defined in:
lib/circuit_breakage/breaker.rb

Overview

A simple circuit breaker implementation. See the main README for usage details.

Direct Known Subclasses

RedisBackedBreaker

Constant Summary collapse

DEFAULT_FAILURE_THRESHOLD =

Number of failures required to trip circuit

5
DEFAULT_DURATION =

Number of seconds the circuit stays tripped

300
DEFAULT_TIMEOUT =

Number of seconds before the call times out

10
DEFAULT_ONLY_TRIP_ON =

Exceptions that trigger the breaker

[Exception]
DEFAULT_NEVER_TRIP_ON =

Exceptions that won’t trigger the breaker

[]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(block = nil) ⇒ Breaker

Returns a new instance of Breaker.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/circuit_breakage/breaker.rb', line 21

def initialize(block=nil)
  self.block              = block
  self.failure_threshold  = DEFAULT_FAILURE_THRESHOLD
  self.duration           = DEFAULT_DURATION
  self.timeout            = DEFAULT_TIMEOUT
  self.only_trip_on       = DEFAULT_ONLY_TRIP_ON
  self.never_trip_on      = DEFAULT_NEVER_TRIP_ON
  self.failure_count      ||= 0
  self.last_failed        ||= 0
  self.state              ||= 'closed'
end

Instance Attribute Details

#blockObject

Returns the value of attribute block.



11
12
13
# File 'lib/circuit_breakage/breaker.rb', line 11

def block
  @block
end

#durationObject

Returns the value of attribute duration.



12
13
14
# File 'lib/circuit_breakage/breaker.rb', line 12

def duration
  @duration
end

#failure_countObject

Returns the value of attribute failure_count.



11
12
13
# File 'lib/circuit_breakage/breaker.rb', line 11

def failure_count
  @failure_count
end

#failure_thresholdObject

Returns the value of attribute failure_threshold.



12
13
14
# File 'lib/circuit_breakage/breaker.rb', line 12

def failure_threshold
  @failure_threshold
end

#last_exceptionObject

Returns the value of attribute last_exception.



12
13
14
# File 'lib/circuit_breakage/breaker.rb', line 12

def last_exception
  @last_exception
end

#last_failedObject

Returns the value of attribute last_failed.



11
12
13
# File 'lib/circuit_breakage/breaker.rb', line 11

def last_failed
  @last_failed
end

#never_trip_onObject

Returns the value of attribute never_trip_on.



13
14
15
# File 'lib/circuit_breakage/breaker.rb', line 13

def never_trip_on
  @never_trip_on
end

#only_trip_onObject

Returns the value of attribute only_trip_on.



13
14
15
# File 'lib/circuit_breakage/breaker.rb', line 13

def only_trip_on
  @only_trip_on
end

#stateObject

Returns the value of attribute state.



11
12
13
# File 'lib/circuit_breakage/breaker.rb', line 11

def state
  @state
end

#timeoutObject

Returns the value of attribute timeout.



12
13
14
# File 'lib/circuit_breakage/breaker.rb', line 12

def timeout
  @timeout
end

Instance Method Details

#call(*args, &block_arg) ⇒ Object

Yield the block within the circuit. If no block is passed use the block passed in at initialization



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/circuit_breakage/breaker.rb', line 35

def call(*args, &block_arg)
  case(state)
  when 'open'
    if time_to_retry?
      do_retry(*args, &block_arg)
    else
      raise CircuitOpen
    end
  when 'closed'
    do_call(*args, &block_arg)
  end
end