Class: Vivarium::EventLog

Inherits:
Object
  • Object
show all
Defined in:
lib/vivarium/api_server.rb

Overview

In-memory, sequence-numbered log of raw event_t records (#EVENT_STRUCT_SIZE bytes each) fed by the daemon’s ring buffer poller and consumed by /events streams.

Instance Method Summary collapse

Constructor Details

#initialize(capacity: 50_000) ⇒ EventLog

Returns a new instance of EventLog.



10
11
12
13
14
15
16
# File 'lib/vivarium/api_server.rb', line 10

def initialize(capacity: 50_000)
  @capacity = capacity
  @mutex = Mutex.new
  @cond = ConditionVariable.new
  @records = []
  @seq = 0
end

Instance Method Details

#append(bytes) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/vivarium/api_server.rb', line 18

def append(bytes)
  @mutex.synchronize do
    @seq += 1
    @records << [@seq, bytes]
    overflow = @records.size - @capacity
    @records.shift(overflow) if overflow.positive?
    @cond.broadcast
  end
end

#read_after(cursor, timeout: 1.0) ⇒ Object

Returns records with seq > cursor. Blocks up to timeout seconds when nothing newer is available so callers can long-poll.



34
35
36
37
38
39
40
41
# File 'lib/vivarium/api_server.rb', line 34

def read_after(cursor, timeout: 1.0)
  @mutex.synchronize do
    if @records.empty? || @records.last[0] <= cursor
      @cond.wait(@mutex, timeout)
    end
    @records.select { |seq, _| seq > cursor }
  end
end

#tail_seqObject



28
29
30
# File 'lib/vivarium/api_server.rb', line 28

def tail_seq
  @mutex.synchronize { @seq }
end