class Gruf::Interceptors::Registry

Handles registration of interceptors

Public Class Methods

new() click to toggle source
# File lib/gruf/interceptors/registry.rb, line 26
def initialize
  @registry = []
end

Public Instance Methods

clear() click to toggle source

Clear the registry

# File lib/gruf/interceptors/registry.rb, line 131
def clear
  interceptors_mutex do
    @registry = []
  end
end
count() click to toggle source

@return [Integer] The number of interceptors currently loaded

# File lib/gruf/interceptors/registry.rb, line 140
def count
  interceptors_mutex do
    @registry ||= []
    @registry.count
  end
end
insert_after(after_class, interceptor_class, options = {}) click to toggle source

Insert an interceptor after another specified interceptor

@param [Class] after_class The interceptor to insert after @param [Class] interceptor_class The class of the interceptor to add @param [Hash] options A hash of options to pass into the interceptor during initialization @raise [InterceptorNotFoundError] if the after interceptor is not found

# File lib/gruf/interceptors/registry.rb, line 87
def insert_after(after_class, interceptor_class, options = {})
  interceptors_mutex do
    pos = @registry.find_index { |opts| opts.fetch(:klass, '') == after_class }
    raise InterceptorNotFoundError if pos.nil?

    @registry.insert(
      (pos + 1),
      klass: interceptor_class,
      options: options
    )
  end
end
insert_before(before_class, interceptor_class, options = {}) click to toggle source

Insert an interceptor before another specified interceptor

@param [Class] before_class The interceptor to insert before @param [Class] interceptor_class The class of the interceptor to add @param [Hash] options A hash of options to pass into the interceptor during initialization @raise [InterceptorNotFoundError] if the before interceptor is not found

# File lib/gruf/interceptors/registry.rb, line 66
def insert_before(before_class, interceptor_class, options = {})
  interceptors_mutex do
    pos = @registry.find_index { |opts| opts.fetch(:klass, '') == before_class }
    raise InterceptorNotFoundError if pos.nil?

    @registry.insert(
      pos,
      klass: interceptor_class,
      options: options
    )
  end
end
list() click to toggle source

Return a list of the interceptor classes in the registry in their execution order

@return [Array<Class>]

# File lib/gruf/interceptors/registry.rb, line 105
def list
  interceptors_mutex do
    @registry.map { |h| h[:klass] }
  end
end
prepare(request, error) click to toggle source

Load and return all interceptors for the given request

@param [Gruf::Controllers::Request] request @param [Gruf::Error] error @return [Array<Gruf::Interceptors::Base>]

# File lib/gruf/interceptors/registry.rb, line 118
def prepare(request, error)
  is = []
  interceptors_mutex do
    @registry.each do |o|
      is << o[:klass].new(request, error, o[:options])
    end
  end
  is
end
remove(interceptor_class) click to toggle source

Remove an interceptor from the registry

@param [Class] interceptor_class The interceptor class to remove @raise [InterceptorNotFoundError] if the interceptor is not found

# File lib/gruf/interceptors/registry.rb, line 51
def remove(interceptor_class)
  pos = @registry.find_index { |opts| opts.fetch(:klass, '') == interceptor_class }
  raise InterceptorNotFoundError if pos.nil?

  @registry.delete_at(pos)
end
use(interceptor_class, options = {}) click to toggle source

Add an interceptor to the registry

@param [Class] interceptor_class The class of the interceptor to add @param [Hash] options A hash of options to pass into the interceptor during initialization

# File lib/gruf/interceptors/registry.rb, line 36
def use(interceptor_class, options = {})
  interceptors_mutex do
    @registry << {
      klass: interceptor_class,
      options: options
    }
  end
end

Private Instance Methods

interceptors_mutex(&block) click to toggle source

Handle mutations to the interceptor registry in a thread-safe manner

# File lib/gruf/interceptors/registry.rb, line 152
def interceptors_mutex(&block)
  @interceptors_mutex ||= begin
    require 'monitor'
    Monitor.new
  end
  @interceptors_mutex.synchronize(&block)
end