Class: Libhoney::Queueing::SizedQueueWithTimeout
- Inherits:
-
Object
- Object
- Libhoney::Queueing::SizedQueueWithTimeout
- Defined in:
- lib/libhoney/queueing/sized_queue_with_timeout.rb
Overview
A queue implementation with optional size limit and optional timeouts on pop and push operations. Heavily influenced / liberally mimicking Avdi Grimm’s Tapas::Queue.
Defined Under Namespace
Classes: PopTimedOut, PushTimedOut
Instance Method Summary collapse
-
#clear ⇒ Object
Removes all objects from the queue.
-
#initialize(max_size = Float::INFINITY, options = {}) ⇒ SizedQueueWithTimeout
constructor
A new instance of SizedQueueWithTimeout.
-
#pop(timeout = :never, &timeout_policy) ⇒ Object
(also: #deq, #shift)
Pop something off the queue.
-
#push(obj, timeout = :never, &timeout_policy) ⇒ Object
(also: #enq, #<<)
Push something onto the queue.
Constructor Details
#initialize(max_size = Float::INFINITY, options = {}) ⇒ SizedQueueWithTimeout
Returns a new instance of SizedQueueWithTimeout.
26 27 28 29 30 31 32 |
# File 'lib/libhoney/queueing/sized_queue_with_timeout.rb', line 26 def initialize(max_size = Float::INFINITY, = {}) @items = [] @max_size = max_size @lock = .fetch(:lock) { QLock.new } @space_available = .fetch(:space_available_condition) { QCondition.new(@lock) } @item_available = .fetch(:item_available_condition) { QCondition.new(@lock) } end |
Instance Method Details
#clear ⇒ Object
Removes all objects from the queue. They are cast into the abyss never to be seen again.
80 81 82 83 84 85 |
# File 'lib/libhoney/queueing/sized_queue_with_timeout.rb', line 80 def clear @lock.synchronize do @items = [] @space_available.signal unless full? end end |
#pop(timeout = :never, &timeout_policy) ⇒ Object Also known as: deq, shift
Pop something off the queue.
65 66 67 68 69 70 71 72 73 |
# File 'lib/libhoney/queueing/sized_queue_with_timeout.rb', line 65 def pop(timeout = :never, &timeout_policy) timeout_policy ||= -> { raise PopTimedOut } wait_for_condition(@item_available, -> { !empty? }, timeout, timeout_policy) do item = @items.shift @space_available.signal unless full? item end end |
#push(obj, timeout = :never, &timeout_policy) ⇒ Object Also known as: enq, <<
Push something onto the queue.
44 45 46 47 48 49 50 51 |
# File 'lib/libhoney/queueing/sized_queue_with_timeout.rb', line 44 def push(obj, timeout = :never, &timeout_policy) timeout_policy ||= -> { raise PushTimedOut } wait_for_condition(@space_available, -> { !full? }, timeout, timeout_policy) do @items.push(obj) @item_available.signal end end |