Class: SwitchGear::CircuitBreaker::Memory

Inherits:
Object
  • Object
show all
Includes:
SwitchGear::CircuitBreaker
Defined in:
lib/switch_gear/circuit_breaker/memory.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SwitchGear::CircuitBreaker

#call, #closed?, #failure_count, #half_open?, #open?

Constructor Details

#initialize {|circuit, failure_limit, reset_timeout, logger| ... } ⇒ SwitchGear::CircuitBreaker::Memory

Returns the object.

Examples:

create a new breaker

breaker = SwitchGear::CircuitBreaker::Memory.new do |cb|
  cb.circuit = -> (arg) { my_method(arg) }
  cb.failure_limit = 2
  cb.reset_timeout = 5
end

Yield Parameters:



38
39
40
41
42
43
44
45
46
# File 'lib/switch_gear/circuit_breaker/memory.rb', line 38

def initialize(&block)
  yield self
  @failure_limit ||= 5
  @reset_timeout ||= 10
  @logger ||= Logger.new(STDOUT)
  @state = :closed
  @failures = []
  run_validations
end

Instance Attribute Details

#circuitProc/Lambda

The main runner, must respond to #call

Returns:

  • (Proc/Lambda)

    the runner



8
9
10
# File 'lib/switch_gear/circuit_breaker/memory.rb', line 8

def circuit
  @circuit
end

#failure_limitInteger

The count of failures

Returns:

  • (Integer)

    the amount of failures to permit. Defaults to 10 seconds.



11
12
13
# File 'lib/switch_gear/circuit_breaker/memory.rb', line 11

def failure_limit
  @failure_limit
end

#failuresArray<CircuitBreaker::Failure>

The current failures

Returns:



24
25
26
# File 'lib/switch_gear/circuit_breaker/memory.rb', line 24

def failures
  @failures
end

#loggerObject

The current logger

Returns:

  • (Object)
    • The logger sent in at initialization. Defaults to ruby's std Logger class.



17
18
19
# File 'lib/switch_gear/circuit_breaker/memory.rb', line 17

def logger
  @logger
end

#reset_timeoutInteger

The amount of time in seconds before a breaker should reset if currently open. Defaults to 5.

Returns:

  • (Integer)


14
15
16
# File 'lib/switch_gear/circuit_breaker/memory.rb', line 14

def reset_timeout
  @reset_timeout
end

#stateSymbol

The current state

Returns:

  • (Symbol)

    should always return either :open, :closed or :half-open. Use the helper methods in lib/circuit_breaker.rb for more readable code.



21
22
23
# File 'lib/switch_gear/circuit_breaker/memory.rb', line 21

def state
  @state
end

Instance Method Details

#add_failure(failure) ⇒ Object

(look to add_failure)



49
50
51
# File 'lib/switch_gear/circuit_breaker/memory.rb', line 49

def add_failure(failure)
  failures << failure
end