Sha256: 1f946f79c6e065ca422b6cfb15488a68623718f05f46268f84118a57931ee5c2

Contents?: true

Size: 1.06 KB

Versions: 2

Compression:

Stored size: 1.06 KB

Contents

require 'thread/pool'
require 'thread/future'
require 'singleton'
require 'delegate'

module Routemaster
  module Responses
    # A pool of threads, used for parallel/future request processing.
    class Pool < SimpleDelegator
      include Singleton

      def initialize
        Thread.pool(5, 20).tap do |p|
          # TODO: configurable pool size and trim timeout?
          p.auto_trim!
          p.idle_trim! 10 # 10 seconds
          super p
        end
      end
    end

    class FutureResponse
      extend Forwardable

      # The `block` is expected to return a {Response}
      def initialize(&block)
        @future = Pool.instance.future(&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 :value => :@future
      delegate %i(status headers body) => :value
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
routemaster-drain-2.3.0 lib/routemaster/responses/future_response.rb
routemaster-drain-2.2.2 lib/routemaster/responses/future_response.rb