Class: Phronomy::Deadline
- Inherits:
-
Object
- Object
- Phronomy::Deadline
- Defined in:
- lib/phronomy/deadline.rb
Overview
A point in time used as an upper bound for an operation.
Uses the monotonic clock (+Process::CLOCK_MONOTONIC+) internally to avoid skew from NTP adjustments or DST transitions.
Class Method Summary collapse
-
.in(seconds) ⇒ Deadline
private
Creates a deadline that expires +seconds+ from now.
Instance Method Summary collapse
-
#attach_to(token, timer_queue: Phronomy::Runtime.instance.timer_queue) ⇒ self
private
Attaches this deadline to a CancellationToken by cancelling the token when the deadline expires.
-
#expired? ⇒ Boolean
private
Returns +true+ when the deadline has passed.
-
#initialize(monotonic_at) ⇒ Deadline
constructor
private
A new instance of Deadline.
-
#remaining_seconds ⇒ Float
private
Seconds remaining until expiry.
Constructor Details
#initialize(monotonic_at) ⇒ Deadline
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Deadline.
26 27 28 |
# File 'lib/phronomy/deadline.rb', line 26 def initialize(monotonic_at) @monotonic_at = monotonic_at end |
Class Method Details
.in(seconds) ⇒ Deadline
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Creates a deadline that expires +seconds+ from now.
20 21 22 |
# File 'lib/phronomy/deadline.rb', line 20 def self.in(seconds) new(Process.clock_gettime(Process::CLOCK_MONOTONIC) + seconds) end |
Instance Method Details
#attach_to(token, timer_queue: Phronomy::Runtime.instance.timer_queue) ⇒ self
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Attaches this deadline to a CancellationToken by cancelling the token when the deadline expires. Uses the Runtime timer queue (a single background thread shared by all deadlines) instead of spawning one thread per deadline.
55 56 57 58 59 60 61 |
# File 'lib/phronomy/deadline.rb', line 55 def attach_to(token, timer_queue: Phronomy::Runtime.instance.timer_queue) seconds = remaining_seconds return self if seconds <= 0 timer_queue.schedule(seconds: seconds) { token.cancel! } self end |
#expired? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns +true+ when the deadline has passed.
33 34 35 |
# File 'lib/phronomy/deadline.rb', line 33 def expired? Process.clock_gettime(Process::CLOCK_MONOTONIC) >= @monotonic_at end |
#remaining_seconds ⇒ Float
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Seconds remaining until expiry. Returns 0 when already expired.
40 41 42 43 |
# File 'lib/phronomy/deadline.rb', line 40 def remaining_seconds remaining = @monotonic_at - Process.clock_gettime(Process::CLOCK_MONOTONIC) [remaining, 0.0].max end |