Class: TgVizor::EventQueue

Inherits:
Object
  • Object
show all
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

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

Returns:

  • (Boolean)


49
50
51
# File 'lib/tgvizor/event_queue.rb', line 49

def any?
  !empty?
end

#clearObject



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

Returns:

  • (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.

Returns:

  • (Boolean)

    true if the queue overflowed (oldest event dropped).



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

#sizeObject



41
42
43
# File 'lib/tgvizor/event_queue.rb', line 41

def size
  @mutex.synchronize { @items.size }
end