Class: Gruf::CircuitBreaker::Hook

Inherits:
Hooks::Base
  • Object
show all
Defined in:
lib/gruf/circuit_breaker/hook.rb

Instance Method Summary collapse

Instance Method Details

#around(call_signature, request, active_call, &block) ⇒ Object

Handle the gruf around hook


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gruf/circuit_breaker/hook.rb', line 36

def around(call_signature, request, active_call, &block)
  light = Stoplight(method_key(call_signature)) do
    block.call(call_signature, request, active_call)
  end
  light.with_cool_off_time(options.fetch(:cool_off_time, 60))
  light.with_error_handler do |error, handle|
    # if it's not a gRPC::BadStatus, pass it through
    raise error unless error.is_a?(GRPC::BadStatus)
    # only special handling for GRPC error statuses. We want to pass-through any normal exceptions
    raise error unless failure_statuses.include?(error.class)
    handle.call(error)
  end
  light.with_threshold(options.fetch(:threshold, 1)) if options.fetch(:threshold, false)
  light.run
end

#setupObject

Sets up the stoplight gem


24
25
26
27
# File 'lib/gruf/circuit_breaker/hook.rb', line 24

def setup
  redis = options.fetch(:redis, nil)
  Stoplight::Light.default_data_store = Stoplight::DataStore::Redis.new(redis) if redis && redis.is_a?(Redis)
end