Class: Farce::Abstract::TimerQueue Abstract
- Includes:
- Internal::BlockingPriorityQueue
- Defined in:
- lib/farce/abstract/timer_queue.rb
Overview
Shared timer scheduling and blocking behavior.
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from Queue
#capacity, #mode, #num_waiting, #size
Instance Method Summary collapse
-
#delete(value, at:, compare_by_identity: false) ⇒ Boolean
Delete the oldest matching value at the exact time.
-
#first_timestamp { ... } ⇒ Float?
Return the earliest timestamp without removing it.
-
#initialize(capacity: nil, track_age: false) ⇒ TimerQueue
constructor
A new instance of TimerQueue.
-
#last_timestamp { ... } ⇒ Float?
Return the latest timestamp without removing it.
-
#overdue?(leeway: 0) ⇒ Boolean
Checks whether the earliest timestamp has been reached.
-
#overdue_by(leeway: 0) ⇒ Float?
Returns how long the earliest timestamp has been overdue, or nil if it is not yet due.
-
#peek { ... } ⇒ BasicObject?
Return the earliest value without removing it, whether or not it is due.
-
#pop(non_block = false, timeout: nil) { ... } ⇒ BasicObject?
Remove the earliest value, waiting until its timestamp is reached.
-
#push(value, non_block = false, timeout: nil, **time_options) ⇒ Boolean
Add a value to become available at the given time.
-
#try_pop { ... } ⇒ BasicObject?
Try to remove the earliest value only if its timestamp has been reached.
-
#try_push(value, **time_options) { ... } ⇒ Boolean, BasicObject
Try to add a value without waiting for capacity.
-
#wait_pop(timeout: nil) ⇒ Boolean
Wait until the earliest timestamp is reached without removing its value.
Methods inherited from Queue
#age_tracking?, #clear, #close, #closed?, #deq, #duplicable?, #empty?, #enq, #full?, #generation, #length, #max, #oldest_age, #oldest_enqueued_at, #seal, #sealed?, #wait_push
Constructor Details
#initialize(capacity: nil, track_age: false) ⇒ TimerQueue
Returns a new instance of TimerQueue.
12 13 14 |
# File 'lib/farce/abstract/timer_queue.rb', line 12 def initialize(capacity: nil, track_age: false) super end |
Instance Method Details
#delete(value, at:, compare_by_identity: false) ⇒ Boolean
Delete the oldest matching value at the exact time.
108 109 110 |
# File 'lib/farce/abstract/timer_queue.rb', line 108 def delete(value, at:, compare_by_identity: false) delete_from_storage(Clock.at(at), value, compare_by_identity:) end |
#first_timestamp { ... } ⇒ Float?
Return the earliest timestamp without removing it.
73 |
# File 'lib/farce/abstract/timer_queue.rb', line 73 def (&) = internal_queue.peek_priority(&) |
#last_timestamp { ... } ⇒ Float?
Return the latest timestamp without removing it.
78 |
# File 'lib/farce/abstract/timer_queue.rb', line 78 def (&) = internal_queue.peek_last_priority(&) |
#overdue?(leeway: 0) ⇒ Boolean
Checks whether the earliest timestamp has been reached.
82 83 84 85 |
# File 'lib/farce/abstract/timer_queue.rb', line 82 def overdue?(leeway: 0) return false unless = <= Clock.in(leeway) end |
#overdue_by(leeway: 0) ⇒ Float?
Returns how long the earliest timestamp has been overdue, or nil if it is not yet due.
90 91 92 93 94 |
# File 'lib/farce/abstract/timer_queue.rb', line 90 def overdue_by(leeway: 0) return unless = delay = Clock.now - delay > leeway ? delay : nil end |
#peek { ... } ⇒ BasicObject?
Return the earliest value without removing it, whether or not it is due.
68 |
# File 'lib/farce/abstract/timer_queue.rb', line 68 def peek(&) = internal_queue.peek(&) |
#pop(non_block = false, timeout: nil) { ... } ⇒ BasicObject?
Remove the earliest value, waiting until its timestamp is reached.
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/farce/abstract/timer_queue.rb', line 35 def pop(non_block = false, timeout: nil) # rubocop:disable Style/OptionalBooleanParameter return try_pop { raise ThreadError, "queue empty" } if non_block deadline = timeout_at(timeout) unless timeout.nil? while true empty = false value = internal_queue.pop_before(Clock.now) { empty = true } return value unless empty return block_given? ? yield : nil if UNDEFINED.equal?((deadline)) end end |
#push(value, non_block = false, timeout: nil, **time_options) ⇒ Boolean
Add a value to become available at the given time.
26 27 28 29 |
# File 'lib/farce/abstract/timer_queue.rb', line 26 def push(value, non_block = false, timeout: nil, **) # rubocop:disable Style/OptionalBooleanParameter at = Clock.parse() push_to_storage(at, non_block, value, timeout:) end |
#try_pop { ... } ⇒ BasicObject?
Try to remove the earliest value only if its timestamp has been reached.
63 |
# File 'lib/farce/abstract/timer_queue.rb', line 63 def try_pop(&) = internal_queue.pop_before(Clock.now, &) |
#try_push(value, **time_options) { ... } ⇒ Boolean, BasicObject
Try to add a value without waiting for capacity.
54 55 56 57 58 |
# File 'lib/farce/abstract/timer_queue.rb', line 54 def try_push(value, **) at = Clock.parse() return true if internal_queue.push(at, value) block_given? ? yield : false end |
#wait_pop(timeout: nil) ⇒ Boolean
Wait until the earliest timestamp is reached without removing its value.
99 100 101 |
# File 'lib/farce/abstract/timer_queue.rb', line 99 def wait_pop(timeout: nil) # rubocop:disable Naming/PredicateMethod !UNDEFINED.equal?((timeout_at(timeout))) end |