Class: Resilient::CircuitBreaker::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/resilient/circuit_breaker/registry.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source = nil) ⇒ Registry

Returns a new instance of Registry.



21
22
23
# File 'lib/resilient/circuit_breaker/registry.rb', line 21

def initialize(source = nil)
  @source = source || {}
end

Class Method Details

.defaultObject

Internal: Default registry to use for circuit breakers.



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

def self.default
  @default
end

.default=(value) ⇒ Object

Internal: Allows overriding default registry for circuit breakers.



10
11
12
# File 'lib/resilient/circuit_breaker/registry.rb', line 10

def self.default=(value)
  @default = value
end

.resetObject

Public: Reset the default registry. This completely wipes all instances by swapping out the default registry for a new one and letting the old one get GC’d. Useful in tests to get a completely clean slate.



17
18
19
# File 'lib/resilient/circuit_breaker/registry.rb', line 17

def self.reset
  default.reset
end

Instance Method Details

#fetch(key, &block) ⇒ Object

Internal: To be used by CircuitBreaker to either get an instance for a key or set a new instance for a key.

Raises KeyError if key not found and no block provided.



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/resilient/circuit_breaker/registry.rb', line 33

def fetch(key, &block)
  if value = @source[key]
    value
  else
    if block_given?
      @source[key] = yield
    else
      @source.fetch(key)
    end
  end
end

#resetObject

Internal: To be used by CircuitBreaker to reset the stored circuit breakers, which should only really be used for cleaning up in test environment.



48
49
50
51
# File 'lib/resilient/circuit_breaker/registry.rb', line 48

def reset
  @source = {}
  nil
end