Class: Async::Deadline
- Inherits:
-
Object
- Object
- Async::Deadline
- Defined in:
- lib/async/deadline.rb
Overview
Represents a deadline timeout with decrementing remaining time. Includes an efficient representation for zero (non-blocking) timeouts.
Defined Under Namespace
Modules: Zero
Class Method Summary collapse
-
.start(timeout) ⇒ Object
Create a deadline for the given timeout.
Instance Method Summary collapse
-
#expired? ⇒ Boolean
Check if the deadline has expired.
-
#initialize(remaining) ⇒ Deadline
constructor
Create a new deadline with the specified remaining time.
-
#remaining ⇒ Object
Get the remaining time, updating internal state.
Constructor Details
Class Method Details
.start(timeout) ⇒ Object
Create a deadline for the given timeout.
33 34 35 36 37 38 39 40 41 |
# File 'lib/async/deadline.rb', line 33 def self.start(timeout) if timeout.nil? nil elsif timeout <= 0 Zero else self.new(timeout) end end |
Instance Method Details
#expired? ⇒ Boolean
Check if the deadline has expired.
66 67 68 |
# File 'lib/async/deadline.rb', line 66 def expired? self.remaining <= 0 end |
#remaining ⇒ Object
Get the remaining time, updating internal state. Each call to this method advances the internal clock and reduces the remaining time by the elapsed duration since the last call.
54 55 56 57 58 59 60 61 62 |
# File 'lib/async/deadline.rb', line 54 def remaining now = Clock.now delta = now - @start @start = now @remaining -= delta return @remaining end |