Sha256: 018b874853a24ce27bb31cf1a4892e1d39552d7d9c876be076e25dfac66c6543

Contents?: true

Size: 796 Bytes

Versions: 1

Compression:

Stored size: 796 Bytes

Contents

module Umbra
  class ShadowRequester
    def initialize(count: 1, pool: 1)
      @count = count
      @pool = pool
      @queue = Queue.new
      @stop = Object.new
      @lock = Mutex.new
    end

    def call(env)
      start_worker!

      request = RequestBuilder.call(env)

      @count.times { @queue.push(request) }
    end

    private

    def start_worker!
      @lock.synchronize do
        return if @started

        @started = true

        workers = (1..@pool).map do |_|
          Thread.new do
            while (request = @queue.pop)
              break if request == @stop

              request.run
            end
          end
        end

        at_exit do
          @pool.times { @queue.push(@stop) }
          workers.map(&:join)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
umbra-rb-0.1.0.pre lib/umbra/shadow_requester.rb