Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 4872c9ad24
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 39 lines (32 sloc) 0.781 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
require 'net/http/persistent'
require 'thread'

class Net::HTTP::Batch

  def initialize batch_size = 10
    @batch_size = batch_size
    @queue = Queue.new
    @results = Queue.new
  end

  def request *args
    @queue << args
    self
  end

  def run
   threads = Array.new(@batch_size) { worker }
    until @queue.empty? && @queue.num_waiting == @batch_size
      threads.each { |t| t.join(0.1) }
    end
    @batch_size.times { @queue << nil }
    threads.each { |t| t.join }
    Array.new(@results.size) { @results.pop }
  end

  private
  def worker
    Thread.new do
      http = Net::HTTP::Persistent.new
      while args = @queue.pop
        @results << http.request(*args)
      end
      # http.shutdown # XXX disabled due to net-http-persistent bug
    end
  end

end
Something went wrong with that request. Please try again.