Skip to content

Instantly share code, notes, and snippets.

@BMorearty
Created February 25, 2020 01:47
Show Gist options
  • Save BMorearty/80427e4c14275963124560cfd97bb889 to your computer and use it in GitHub Desktop.
Save BMorearty/80427e4c14275963124560cfd97bb889 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'async'
require 'kernel/sync'
require 'async/http'
require 'protocol/http'
endpoint = Async::HTTP::Endpoint.new(URI.parse('https://www.airbnb.com'))
client = Async::HTTP::Client.new(endpoint)
request = Protocol::HTTP::Request['GET', '/help/home', Protocol::HTTP::Headers[{}], nil]
Sync do
5.times do |i|
Async do
response = client.call(request)
body = response.read
puts "/help/home #{i}: status #{response.status}, sample: #{body.split("\n").grep(/doctype/i)[0]}"
end
end
end
@BMorearty
Copy link
Author

#!/usr/bin/env ruby

require 'async'
require 'kernel/sync'
require 'async/http'
require 'protocol/http'

endpoint = Async::HTTP::Endpoint.new(URI.parse('https://www.airbnb.com'))
client = Async::HTTP::Client.new(endpoint)
request = Protocol::HTTP::Request['GET', '/help/home', Protocol::HTTP::Headers[{}], nil]

Sync do
  5.times do |i|
    Async do
      response = client.call(request)
      body = response.read
      puts "/help/home #{i}: status #{response.status}, sample: #{body.split("\n").grep(/doctype/i)[0]}"
    end.wait
  end
end.wait

client.close

@BMorearty
Copy link
Author

#!/usr/bin/env ruby

require 'async'
require 'async/barrier'
require 'kernel/sync'
require 'async/http'
require 'protocol/http'

endpoint = Async::HTTP::Endpoint.new(URI.parse('https://www.airbnb.com'))
client = Async::HTTP::Client.new(endpoint)
request = Protocol::HTTP::Request['GET', '/help/home', Protocol::HTTP::Headers[{}], nil]

Sync do
  barrier = Async::Barrier.new
  5.times do |i|
    barrier.async do
      response = client.call(request)
      body = response.read
      puts "/help/home #{i}: status #{response.status}, sample: #{body.split("\n").grep(/doctype/i)[0]}"
    end
  end
  barrier.wait
  client.close
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment