Class: AsyncLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/qravan/async_loader.rb

Class Method Summary collapse

Class Method Details

.perform(urls) ⇒ Object



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
# File 'lib/qravan/async_loader.rb', line 6

def self.perform(urls)
  internet = Async::HTTP::Internet.new
  barrier = Async::Barrier.new
  time = Time.now
  requests = 0
  result = []
  urls.each do |url|
    barrier.async do
      Console.logger.info "AsyncHttp#get: #{url}"
      begin
        response = internet.get(url)
        body = JSON.parse(response.read)
        result << { url: url, body: body }
        requests += 1
      ensure
        response.finish
      end
      Console.logger.info "AsyncHttp#fulfill: #{url}"
    end
  end

  Console.logger.info "AsyncHttp#wait"
  barrier.wait
  total = Time.now - time
  result.unshift({time: "Duration: #{Time.now - time}s for #{requests} (RPS: #{(requests / total).to_i} r/s)"})
  result.to_json
end