Skip to content

Instantly share code, notes, and snippets.

@ocher
Created October 17, 2014 17:56
Show Gist options
  • Save ocher/750f5628241dcf25a6fe to your computer and use it in GitHub Desktop.
Save ocher/750f5628241dcf25a6fe to your computer and use it in GitHub Desktop.
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