Skip to content

Instantly share code, notes, and snippets.

@chrisbnt
Created August 31, 2011 00:59
Show Gist options
  • Save chrisbnt/1182562 to your computer and use it in GitHub Desktop.
Save chrisbnt/1182562 to your computer and use it in GitHub Desktop.
How to handle timeouts decently with em-http-request 0.3.0
require 'eventmachine'
require 'em-http'
EM::run do
rq = EM::HttpRequest.new("http://www.slow.com").get
def rq.timed_out?
@timed_out
end
timeout = EM::Timer.new(1) do
rq.instance_variable_set :@timed_out, true
rq.unbind
end
rq.callback do |*args|
timeout.cancel
puts "callback #{args.inspect}"
end
rq.errback do |*args|
timeout.cancel
if rq.timed_out?
puts "timeout in errback"
else
puts "errback #{args.inspect}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment