Class: CircuitBreaker::Redis

Inherits:
Object
  • Object
show all
Includes:
CircuitBreaker
Defined in:
lib/circuit_breaker/redis.rb

Constant Summary

Constants included from CircuitBreaker

VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CircuitBreaker

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

Constructor Details

#initialize {|circuit, failure_limit, reset_timeout, logger, client, namespace| ... } ⇒ CircuitBreaker::Redis

The main class to instantiate the CircuitBraker class.

Examples:

create a new breaker

breaker = 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:



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

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)



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

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



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

def client
  @client
end

#failure_limitObject



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

def failure_limit
  @failure_limit
end

#failuresObject

(look to Memory#failures)



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

def failures
  @failures
end

#loggerObject

(look to Memory#logger)



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

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



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

def namespace
  @namespace
end

#reset_timeoutObject



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

def reset_timeout
  @reset_timeout
end

#stateObject

(look to Memory#state)



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

def state
  @state
end

Instance Method Details

#add_failure(failure) ⇒ Object



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

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