Class: TgVizor::EventQueue
- Inherits:
-
Object
- Object
- TgVizor::EventQueue
- Defined in:
- lib/tgvizor/event_queue.rb
Overview
Thread-safe in-memory event queue with batch draining.
Events are pushed one at a time from any thread (handler thread, error callbacks) and drained in batches by the background flush thread. When the queue exceeds max_size, the oldest events are dropped to bound memory usage in incident scenarios where ingestion is unreachable.
Instance Method Summary collapse
- #any? ⇒ Boolean
- #clear ⇒ Object
-
#drain(batch_size) ⇒ Object
Remove and return up to
batch_sizeevents from the front. - #empty? ⇒ Boolean
-
#initialize(max_size) ⇒ EventQueue
constructor
A new instance of EventQueue.
-
#push(event) ⇒ Boolean
Push an event to the queue.
- #size ⇒ Object
Constructor Details
#initialize(max_size) ⇒ EventQueue
Returns a new instance of EventQueue.
13 14 15 16 17 |
# File 'lib/tgvizor/event_queue.rb', line 13 def initialize(max_size) @items = [] @max_size = max_size @mutex = Mutex.new end |
Instance Method Details
#any? ⇒ Boolean
49 50 51 |
# File 'lib/tgvizor/event_queue.rb', line 49 def any? !empty? end |
#clear ⇒ Object
53 54 55 |
# File 'lib/tgvizor/event_queue.rb', line 53 def clear @mutex.synchronize { @items.clear } end |
#drain(batch_size) ⇒ Object
Remove and return up to batch_size events from the front. Caller takes ownership of the returned array.
35 36 37 38 39 |
# File 'lib/tgvizor/event_queue.rb', line 35 def drain(batch_size) @mutex.synchronize do @items.shift(batch_size) end end |
#empty? ⇒ Boolean
45 46 47 |
# File 'lib/tgvizor/event_queue.rb', line 45 def empty? size.zero? end |
#push(event) ⇒ Boolean
Push an event to the queue.
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/tgvizor/event_queue.rb', line 21 def push(event) @mutex.synchronize do @items << event if @items.size > @max_size @items.shift true else false end end end |
#size ⇒ Object
41 42 43 |
# File 'lib/tgvizor/event_queue.rb', line 41 def size @mutex.synchronize { @items.size } end |