Sha256: 5172eef7084853ddbbdf7a20a658df7345d49c92caff6bc31166f2c8feed4ad5

Contents?: true

Size: 1.59 KB

Versions: 8

Compression:

Stored size: 1.59 KB

Contents

require 'concurrent/promise'
require 'concurrent/executor/cached_thread_pool'
require 'singleton'
require 'delegate'

module Routemaster
  module Responses
    class ResponsePromise
      extend Forwardable

      # The `block` is expected to return a {Response}
      def initialize(&block)
        @promise = Concurrent::Promise.execute(executor: Pool.current, &block)
      end

      # @!attribute status
      # @return [Integer]
      # Delegated to the `block`'s return value.

      # @!attribute headers
      # @return [Hash]
      # Delegated to the `block`'s return value.

      # @!attribute body
      # @return [Hashie::Mash]
      # Delegated to the `block`'s return value.
      delegate %i(status headers body) => :value
      delegate %i(on_success on_error execute state) => :@promise

      delegate :respond_to_missing? => :value

      def method_missing(m, *args, &block)
        value.public_send(m, *args, &block)
      end

      def value
        @promise.value.tap do
          raise @promise.reason if @promise.rejected?
        end
      end

      module Pool
        LOCK = Mutex.new

        def self.current
          LOCK.synchronize do
            @pool ||= _build_pool
          end
        end

        def self.reset
          LOCK.synchronize do
            return unless @pool
            @pool.tap(&:shutdown).wait_for_termination
            @pool = nil
          end
          self
        end

        def self._build_pool
          Concurrent::CachedThreadPool.new(min_length: 5, max_length: 20, max_queue: 0, fallback_policy: :caller_runs)
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
routemaster-drain-2.5.3 lib/routemaster/responses/response_promise.rb
routemaster-drain-2.5.2 lib/routemaster/responses/response_promise.rb
routemaster-drain-2.5.1 lib/routemaster/responses/response_promise.rb
routemaster-drain-2.5.0 lib/routemaster/responses/response_promise.rb
routemaster-drain-2.4.4 lib/routemaster/responses/response_promise.rb
routemaster-drain-2.4.3 lib/routemaster/responses/response_promise.rb
routemaster-drain-2.4.2 lib/routemaster/responses/response_promise.rb
routemaster-drain-2.4.1 lib/routemaster/responses/response_promise.rb