Class: AsyncStorage::CircuitBreaker

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

Overview

This is not a real circuit breaker. We can improve it later. Basicaly only call the fallback function when some known exception is thrown

Instance Method Summary collapse

Constructor Details

#initialize(context, exceptions: []) ⇒ CircuitBreaker

Returns a new instance of CircuitBreaker.



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

def initialize(context, exceptions: [])
  @context = context
  @exceptions = exceptions || []
end

Instance Method Details

#run(fallback: nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/async_storage/circuit_breaker.rb', line 14

def run(fallback: nil)
  func = fallback.is_a?(Proc) ? fallback : Proc.new { fallback }
  yield
rescue => err
  if exception?(err)
    func.arity == 0 ? @context.instance_exec(&func) : func.call(@context)
  else
    raise(err)
  end
end