Skip to content

Instantly share code, notes, and snippets.

@crabvk
Last active November 6, 2015 08:36
Show Gist options
  • Save crabvk/557d7827e595a6efee76 to your computer and use it in GitHub Desktop.
Save crabvk/557d7827e595a6efee76 to your computer and use it in GitHub Desktop.
Ruby event machine http request read timeout line Net::HTTP#read_timeout
require 'em-synchrony'
require 'em-synchrony/em-http'
require 'em-synchrony/fiber_iterator'
class TimeoutMiddleware
def request(client, head, body)
client.timeout(15)
[head, body]
end
end
header = {
'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:40.0) Gecko/20100101 Firefox/40.0',
'Accept' => '*/*'
}
urls = ['http://tuvietthao1.koding.io/ua.php'] # endless loading page
concurrency = 10
EM.synchrony do
EM::HttpRequest.use(TimeoutMiddleware)
EM::Synchrony::FiberIterator.new(urls, concurrency).each do |url|
conn = EM::HttpRequest.new(url, connect_timeout: 5, inactivity_timeout: 10)
resp = conn.get(redirects: 2, head: header)
if resp.finished?
puts resp.response
else
puts 'error'
# resp.response is also available here
end
end
EM.stop
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment