-
-
Save andyfleming/f9e2a858579a7561f1d0248f1fffb693 to your computer and use it in GitHub Desktop.
Multiple Requests Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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