Created
October 17, 2014 17:56
-
-
Save ocher/750f5628241dcf25a6fe to your computer and use it in GitHub Desktop.
This file contains hidden or 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
if defined?(JRUBY_VERSION) | |
# Based on: https://gist.github.com/jorgenpt/1356797 | |
# unfortunately it opens a new thread each time timeout is invoked :-/ | |
class HTTPClient | |
module Timeout | |
def timeout(sec, klass=nil) | |
return yield(sec) if sec == nil or sec.zero? | |
thread = Thread.new { yield(sec) } | |
if thread.join(sec).nil? | |
java_thread = JRuby.reference(thread) | |
thread.kill | |
java_thread.native_thread.interrupt | |
thread.join(0.1) | |
raise (klass || TimeoutError), 'execution expired' | |
else | |
thread.value | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment