class AsyncStorage::CircuitBreaker

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

@see martinfowler.com/bliki/CircuitBreaker.html

Public Class Methods

new(context, exceptions: []) click to toggle source
# File lib/async_storage/circuit_breaker.rb, line 9
def initialize(context, exceptions: [])
  @context = context
  @exceptions = exceptions || []
end

Public Instance Methods

run(fallback: nil) { || ... } click to toggle source
# 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

Private Instance Methods

exception?(error) click to toggle source
# File lib/async_storage/circuit_breaker.rb, line 27
def exception?(error)
  AsyncStorage.config.circuit_breaker? && \
    (@exceptions.empty? || @exceptions.any? { |known_error| error.is_a?(known_error) })
end