class Gruf::CircuitBreaker::Interceptor

Intercepts gruf requests and adds a circuit breaker implementation

Public Class Methods

new(request, error, options = {}) click to toggle source

Sets up the stoplight gem

Calls superclass method
# File lib/gruf/circuit_breaker/interceptor.rb, line 25
def initialize(request, error, options = {})
  redis = options.fetch(:redis, nil)
  Stoplight::Light.default_data_store = Stoplight::DataStore::Redis.new(redis) if redis && redis.is_a?(Redis)
  super(request, error, options)
end

Public Instance Methods

call() { || ... } click to toggle source

Handle the gruf around hook

# File lib/gruf/circuit_breaker/interceptor.rb, line 34
def call
  light = Stoplight(request.method_key) do
    yield
  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(threshold) if threshold > 0
  light.run
end

Private Instance Methods

failure_statuses() click to toggle source

@return [Array<Class>]

# File lib/gruf/circuit_breaker/interceptor.rb, line 62
def failure_statuses
  options.fetch(:failure_statuses, [GRPC::Internal, GRPC::Unknown])
end
threshold() click to toggle source

@return [Integer]

# File lib/gruf/circuit_breaker/interceptor.rb, line 55
def threshold
  options.fetch(:threshold, 0).to_i
end