Sha256: 406ecdea1db04b24c6ef3a10359d66758cb015b3df4547903a3623a22223452c
Contents?: true
Size: 1.61 KB
Versions: 22
Compression:
Stored size: 1.61 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::ThreadPoolExecutor.new(min_length: 5, max_length: 20, max_queue: 0, max_threads: 20, fallback_policy: :caller_runs) end end end end end
Version data entries
22 entries across 22 versions & 1 rubygems