Module: SwitchGear::CircuitBreaker

Included in:
Memory, Redis
Defined in:
lib/switch_gear/circuit_breaker.rb,
lib/switch_gear/circuit_breaker/redis.rb,
lib/switch_gear/circuit_breaker/memory.rb,
lib/switch_gear/circuit_breaker/failure.rb,
lib/switch_gear/circuit_breaker/open_error.rb

Defined Under Namespace

Classes: Failure, Memory, OpenError, Redis

Instance Method Summary collapse

Instance Method Details

#add_failure(failure) ⇒ void

This method returns an undefined value.

Parameters:



57
58
59
# File 'lib/switch_gear/circuit_breaker.rb', line 57

def add_failure(failure)
  must_implement(:add_failure)
end

#call(*args) ⇒ Void, SwitchGear::CircuitBreaker::Open

Calls the circuit proc/lambda if the circuit is closed or half-open.

Parameters:

  • args (Array<Object>)

    Any number of Objects to be called with the circuit block.

Returns:

  • (Void, SwitchGear::CircuitBreaker::Open)

    No usable return value if successful, but will raise an error if failure_limit is reached or if the circuit is open

Raises:



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

def call(*args)
  check_reset_timeout
  raise OpenError if open?
  do_run(args, &circuit)
end

#closed?Boolean

Returns Whether the circuit is closed.

Returns:

  • (Boolean)

    Whether the circuit is closed



35
36
37
# File 'lib/switch_gear/circuit_breaker.rb', line 35

def closed?
  state == :closed
end

#failure_countInteger

Returns The count of current failures.

Returns:

  • (Integer)

    The count of current failures



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

def failure_count
  failures.size
end

#failuresArray<SwitchGear::CircuitBreaker::Failure>

Returns a list of current failures.

Returns:



45
46
47
# File 'lib/switch_gear/circuit_breaker.rb', line 45

def failures
  must_implement(:failures)
end

#failures=(failure) ⇒ void

This method returns an undefined value.

Parameters:



51
52
53
# File 'lib/switch_gear/circuit_breaker.rb', line 51

def failures=(failure)
  must_implement(:failures=)
end

#half_open?Boolean

Returns Whether the circuit is half-open.

Returns:

  • (Boolean)

    Whether the circuit is half-open



40
41
42
# File 'lib/switch_gear/circuit_breaker.rb', line 40

def half_open?
  state == :half_open
end

#most_recent_failureObject

Returns most recent failure [SwitchGear::CircuitBreaker::Failure].

Returns:

  • most recent failure [SwitchGear::CircuitBreaker::Failure]



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

def most_recent_failure
  failures.last
end

#open?Boolean

Returns Whether the circuit is open.

Returns:

  • (Boolean)

    Whether the circuit is open



30
31
32
# File 'lib/switch_gear/circuit_breaker.rb', line 30

def open?
  state == :open
end

#stateSymbol

Returns - either :open, :closed, :half-open.

Returns:

  • (Symbol)
    • either :open, :closed, :half-open



62
63
64
# File 'lib/switch_gear/circuit_breaker.rb', line 62

def state
  must_implement(:state)
end

#state=(state) ⇒ void

This method returns an undefined value.

Parameters:

  • state (Symbol)
    • either :open, :closed, :half-open



68
69
70
# File 'lib/switch_gear/circuit_breaker.rb', line 68

def state=(state)
  must_implement(:state=)
end