Created
August 31, 2011 00:59
-
-
Save chrisbnt/1182562 to your computer and use it in GitHub Desktop.
How to handle timeouts decently with em-http-request 0.3.0
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 '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