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
Local::TimerQueue, Strict::TimerQueue, TimerQueue, Unshared::TimerQueue
Defined Under Namespace
Classes: Entry
Instance Attribute Summary
Attributes inherited from Queue
#capacity, #mode, #num_waiting, #size
Instance Method Summary collapse
-
#delete(value, at: UNDEFINED, compare_by_identity: false) ⇒ Boolean
Cancel an entry, or 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) ⇒ Entry, false
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) { ... } ⇒ Entry, 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.
43 44 45 |
# File 'lib/farce/abstract/timer_queue.rb', line 43 def initialize(capacity: nil, track_age: false) super end |
Instance Method Details
#delete(value, at: UNDEFINED, compare_by_identity: false) ⇒ Boolean
Cancel an entry, or delete the oldest matching value at the exact time.
Pass at: to delete an Entry that was itself queued as a value.
143 144 145 146 147 148 149 |
# File 'lib/farce/abstract/timer_queue.rb', line 143 def delete(value, at: UNDEFINED, compare_by_identity: false) if UNDEFINED.equal?(at) raise ArgumentError, "at: is required when deleting a value" unless Entry === value return value.queue.equal?(self) && value.delete end delete_from_storage(Clock.at(at), value, compare_by_identity:) end |
#first_timestamp { ... } ⇒ Float?
Return the earliest timestamp without removing it.
106 |
# File 'lib/farce/abstract/timer_queue.rb', line 106 def (&) = internal_queue.peek_priority(&) |
#last_timestamp { ... } ⇒ Float?
Return the latest timestamp without removing it.
111 |
# File 'lib/farce/abstract/timer_queue.rb', line 111 def (&) = internal_queue.peek_last_priority(&) |
#overdue?(leeway: 0) ⇒ Boolean
Checks whether the earliest timestamp has been reached.
115 116 117 118 |
# File 'lib/farce/abstract/timer_queue.rb', line 115 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.
123 124 125 126 127 |
# File 'lib/farce/abstract/timer_queue.rb', line 123 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.
101 |
# File 'lib/farce/abstract/timer_queue.rb', line 101 def peek(&) = internal_queue.peek(&) |
#pop(non_block = false, timeout: nil) { ... } ⇒ BasicObject?
Remove the earliest value, waiting until its timestamp is reached.
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/farce/abstract/timer_queue.rb', line 67 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) ⇒ Entry, false
Add a value to become available at the given time.
57 58 59 60 61 |
# File 'lib/farce/abstract/timer_queue.rb', line 57 def push(value, non_block = false, timeout: nil, **) # rubocop:disable Style/OptionalBooleanParameter at = Clock.parse() entry = Entry.new(value, at, self) push_to_storage(at, non_block, value, timeout:) && entry end |
#try_pop { ... } ⇒ BasicObject?
Try to remove the earliest value only if its timestamp has been reached.
96 |
# File 'lib/farce/abstract/timer_queue.rb', line 96 def try_pop(&) = internal_queue.pop_before(Clock.now, &) |
#try_push(value, **time_options) { ... } ⇒ Entry, BasicObject
Try to add a value without waiting for capacity.
86 87 88 89 90 91 |
# File 'lib/farce/abstract/timer_queue.rb', line 86 def try_push(value, **) at = Clock.parse() entry = Entry.new(value, at, self) return entry 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.
132 133 134 |
# File 'lib/farce/abstract/timer_queue.rb', line 132 def wait_pop(timeout: nil) # rubocop:disable Naming/PredicateMethod !UNDEFINED.equal?((timeout_at(timeout))) end |