Class: Logtail::LogDevices::HTTP::FlushableDroppingSizedQueue
- Inherits:
-
Object
- Object
- Logtail::LogDevices::HTTP::FlushableDroppingSizedQueue
- Defined in:
- lib/logtail/log_devices/http/flushable_dropping_sized_queue.rb
Overview
A simple thread-safe queue implementation that provides a #flush method. The built-in ruby ‘Queue` class does not provide a #flush method that allows the caller to retrieve all items on the queue in one call. The Ruby `SizedQueue` also implements thread waiting, which is something we want to avoid. To keep things simple and straight-forward, we designed this queue class.
Instance Method Summary collapse
-
#deq ⇒ Object
Removes a single item from the queue.
-
#enq(msg) ⇒ Object
Adds a message to the queue.
-
#flush ⇒ Object
Flushes all message from the queue and returns them.
- #full? ⇒ Boolean
-
#initialize(max_size) ⇒ FlushableDroppingSizedQueue
constructor
A new instance of FlushableDroppingSizedQueue.
- #size ⇒ Object
Constructor Details
#initialize(max_size) ⇒ FlushableDroppingSizedQueue
Returns a new instance of FlushableDroppingSizedQueue.
11 12 13 14 15 |
# File 'lib/logtail/log_devices/http/flushable_dropping_sized_queue.rb', line 11 def initialize(max_size) @lock = Mutex.new @max_size = max_size @array = [] end |
Instance Method Details
#deq ⇒ Object
Removes a single item from the queue
27 28 29 30 31 |
# File 'lib/logtail/log_devices/http/flushable_dropping_sized_queue.rb', line 27 def deq @lock.synchronize do @array.pop end end |
#enq(msg) ⇒ Object
Adds a message to the queue
18 19 20 21 22 23 24 |
# File 'lib/logtail/log_devices/http/flushable_dropping_sized_queue.rb', line 18 def enq(msg) @lock.synchronize do if !full? @array << msg end end end |
#flush ⇒ Object
Flushes all message from the queue and returns them.
34 35 36 37 38 39 40 |
# File 'lib/logtail/log_devices/http/flushable_dropping_sized_queue.rb', line 34 def flush @lock.synchronize do old = @array @array = [] return old end end |
#full? ⇒ Boolean
42 43 44 |
# File 'lib/logtail/log_devices/http/flushable_dropping_sized_queue.rb', line 42 def full? size >= @max_size end |
#size ⇒ Object
46 47 48 |
# File 'lib/logtail/log_devices/http/flushable_dropping_sized_queue.rb', line 46 def size @array.size end |