Sha256: 1d1be90b88737dfe2c085ef40d6ff7aa1ae04854a77558931863723212983693

Contents?: true

Size: 1.19 KB

Versions: 7

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

require "anycable/middleware"
require "monitor"

module AnyCable
  # Middleware chain is used to build the list of
  # gRPC server interceptors.
  #
  # Each interceptor should be a subsclass of
  # AnyCable::Middleware and implement `#call` method.
  class MiddlewareChain
    def initialize
      @registry = []
      @mu = Monitor.new
    end

    def use(middleware)
      check_frozen!
      middleware = build_middleware(middleware)
      sync { registry << middleware }
    end

    def freeze
      registry.freeze
      super
    end

    def to_a
      registry
    end

    private

    attr_reader :mu, :registry

    def sync
      mu.synchronize { yield }
    end

    def check_frozen!
      raise "Cannot modify AnyCable middlewares after server started" if frozen?
    end

    def build_middleware(middleware)
      middleware = middleware.new if
        middleware.is_a?(Class) && middleware <= AnyCable::Middleware

      unless middleware.is_a?(AnyCable::Middleware)
        raise ArgumentError,
              "AnyCable middleware must be a subclass of AnyCable::Middleware, " \
              "got #{middleware} instead"
      end

      middleware
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
anycable-0.6.5 lib/anycable/middleware_chain.rb
anycable-0.6.4 lib/anycable/middleware_chain.rb
anycable-0.6.3 lib/anycable/middleware_chain.rb
anycable-0.6.2 lib/anycable/middleware_chain.rb
anycable-0.6.1 lib/anycable/middleware_chain.rb
anycable-0.6.0 lib/anycable/middleware_chain.rb
anycable-0.6.0.rc1 lib/anycable/middleware_chain.rb