Sha256: da8539fbe42c2a14487b45249301e1a21b3ae63820864e755a77d29b41d900cb

Contents?: true

Size: 1.41 KB

Versions: 13

Compression:

Stored size: 1.41 KB

Contents

module EventMachine

  # EventMachine based Multi request client, based on a streaming HTTPRequest class,
  # which allows you to open multiple parallel connections and return only when all
  # of them finish. (i.e. ideal for parallelizing workloads)
  #
  # == Example
  #
  #  EventMachine.run {
  #
  #    multi = EventMachine::MultiRequest.new
  #
  #    # add multiple requests to the multi-handler
  #    multi.add(:a, EventMachine::HttpRequest.new('http://www.google.com/').get)
  #    multi.add(:b, EventMachine::HttpRequest.new('http://www.yahoo.com/').get)
  #
  #    multi.callback {
  #      p multi.responses[:callback]
  #      p multi.responses[:errback]
  #
  #      EventMachine.stop
  #    }
  #  }
  #

  class MultiRequest
    include EventMachine::Deferrable

    attr_reader :requests, :responses

    def initialize
      @requests  = {}
      @responses = {:callback => {}, :errback => {}}
    end

    def add(name, conn)
      raise 'Duplicate Multi key' if @requests.key? name

      @requests[name] = conn

      conn.callback { @responses[:callback][name] = conn; check_progress }
      conn.errback  { @responses[:errback][name]  = conn; check_progress }
    end

    def finished?
      (@responses[:callback].size + @responses[:errback].size) == @requests.size
    end

    protected

    # invoke callback if all requests have completed
    def check_progress
      succeed(self) if finished?
    end

  end
end

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
em-http-request-samesite-0.1.1 lib/em-http/multi.rb
em-http-request-samesite-1.1.7 lib/em-http/multi.rb
em-http-request-1.1.7 lib/em-http/multi.rb
em-http-request-1.1.6 lib/em-http/multi.rb
em-http-request-1.1.5 lib/em-http/multi.rb
em-http-request-1.1.4 lib/em-http/multi.rb
em-http-request-1.1.3 lib/em-http/multi.rb
em-http-request-1.1.2 lib/em-http/multi.rb
em-http-request-1.1.1 lib/em-http/multi.rb
em-http-request-1.1.0 lib/em-http/multi.rb
em-http-request-1.0.3 lib/em-http/multi.rb
em-http-request-1.0.2 lib/em-http/multi.rb
em-http-request-1.0.1 lib/em-http/multi.rb