Module: FrugalTimeout
- Defined in:
- lib/frugal_timeout.rb
Overview
– {{{1 Rdoc ++ Timeout.timeout() replacement using only 1 thread
Example
require 'frugal_timeout'
begin
FrugalTimeout.timeout(0.1) { sleep }
rescue Timeout::Error
puts 'it works!'
end
# Ensure that calling timeout() will use FrugalTimeout.timeout().
FrugalTimeout.dropin!
# Rescue frugal-specific exception if needed.
begin
timeout(0.1) { sleep }
rescue FrugalTimeout::Error
puts 'yay!'
end
– }}}1
Defined Under Namespace
Classes: Error, MonotonicTime, Request, RequestQueue, SleeperNotifier, SortedQueue
Class Method Summary collapse
-
.dropin! ⇒ Object
Ensure that calling timeout() will use FrugalTimeout.timeout().
-
.on_ensure(&b) ⇒ Object
:nodoc:.
-
.timeout(sec, klass = Error) ⇒ Object
Same as Timeout.timeout().
Class Method Details
.dropin! ⇒ Object
Ensure that calling timeout() will use FrugalTimeout.timeout()
288 289 290 291 292 293 |
# File 'lib/frugal_timeout.rb', line 288 def self.dropin! Object.class_eval \ 'def timeout t, klass=Error, &b FrugalTimeout.timeout t, klass, &b end' end |
.on_ensure(&b) ⇒ Object
:nodoc:
295 296 297 |
# File 'lib/frugal_timeout.rb', line 295 def self.on_ensure &b #:nodoc: @onEnsure = b end |
.timeout(sec, klass = Error) ⇒ Object
Same as Timeout.timeout()
300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/frugal_timeout.rb', line 300 def self.timeout sec, klass=Error return yield sec if sec.nil? || sec <= 0 innerException = Class.new Timeout::ExitException request = @requestQueue.queue(sec, innerException) begin yield sec rescue innerException => e raise klass, e., e.backtrace ensure @onEnsure.call if @onEnsure request.defuse! end end |