Class: SwitchGear::CircuitBreaker::Redis

Inherits:
Object
  • Object
show all
Includes:
SwitchGear::CircuitBreaker
Defined in:
lib/switch_gear/circuit_breaker/redis.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, client, namespace| ... } ⇒ SwitchGear::CircuitBreaker::Redis

The main class to instantiate the CircuitBraker class.

Examples:

create a new breaker

breaker = SwitchGear::CircuitBreaker::Redis.new do |cb|
  cb.circuit = -> (arg) { my_method(arg) }
  cb.failure_limit = 2
  cb.reset_timeout = 5
  cb.client = redis_client
  cb.namespace = "some_key"
end

Yield Parameters:



49
50
51
52
53
54
55
56
57
58
# File 'lib/switch_gear/circuit_breaker/redis.rb', line 49

def initialize
  yield self
  @client = client
  @namespace = namespace
  @failure_limit ||= 5
  @reset_timeout ||= 10
  @logger = Logger.new(STDOUT)
  run_validations
  @namespace = "circuit_breaker:#{namespace}"
end

Instance Attribute Details

#circuitObject

(look to Memory#circuit)



6
7
8
# File 'lib/switch_gear/circuit_breaker/redis.rb', line 6

def circuit
  @circuit
end

#clientObject

An instance of an redis client. This library does not have a hard dependency on a particular redis client but for testing I've used [redis-rb](github.com/redis/redis-rb). Whatever you pass in here simply has to implement a few redis commands such as `sadd`, `del`, `smembers`, `get` and `set`. The client will ensure these exist before the breaker can be instantiated.

Returns:

  • (Object)
    • redis client given



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

def client
  @client
end

#failure_limitObject



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

def failure_limit
  @failure_limit
end

#failuresObject

(look to Memory#failures)



16
17
18
# File 'lib/switch_gear/circuit_breaker/redis.rb', line 16

def failures
  @failures
end

#loggerObject

(look to Memory#logger)



12
13
14
# File 'lib/switch_gear/circuit_breaker/redis.rb', line 12

def logger
  @logger
end

#namespaceString

A unique name that will be used across servers to sync state and failures. I'd recommend `your_class.name:your_method_name` or whatever is special about what's being invoked in the `circuit`. See examples/example_redis.rb

Returns:

  • (String)
    • namespace given



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

def namespace
  @namespace
end

#reset_timeoutObject



10
11
12
# File 'lib/switch_gear/circuit_breaker/redis.rb', line 10

def reset_timeout
  @reset_timeout
end

#stateObject

(look to Memory#state)



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

def state
  @state
end

Instance Method Details

#add_failure(failure) ⇒ Object



79
80
81
# File 'lib/switch_gear/circuit_breaker/redis.rb', line 79

def add_failure(failure)
  client.sadd(fail_namespace, failure.to_json)
end