Skip to content

Instantly share code, notes, and snippets.

@andyfleming
Last active December 30, 2016 23:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andyfleming/f9e2a858579a7561f1d0248f1fffb693 to your computer and use it in GitHub Desktop.
Save andyfleming/f9e2a858579a7561f1d0248f1fffb693 to your computer and use it in GitHub Desktop.
Multiple Requests Example
require "HTTP"
API_BASE_URL = "http://localhost:9200"
start = Time.now.epoch_ms()
channel = Channel({Int32, String}).new
["/one", "/two", "/three"].each_with_index do |endpoint, index|
spawn do
channel.send({index, HTTP::Client.get(API_BASE_URL + endpoint).body})
end
end
puts [channel.receive(), channel.receive(), channel.receive()].sort { |a, b| a <=> b }.map { |el| el[1] }
puts "Completed in " + (Time.now.epoch_ms() - start).to_s + " ms"
require "http/client"
API_BASE_URL = "http://localhost:9200"
start = Time.now.epoch_ms()
results = ["/one","/two","/three"].map do |endpoint|
future do
HTTP::Client.get("#{API_BASE_URL}#{endpoint}").body
end
end.map(&.get)
puts results
puts "Completed in " + (Time.now.epoch_ms() - start).to_s + " ms"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment