Class: Vivarium::EventLog
- Inherits:
-
Object
- Object
- Vivarium::EventLog
- 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
- #append(bytes) ⇒ Object
-
#initialize(capacity: 50_000) ⇒ EventLog
constructor
A new instance of EventLog.
-
#read_after(cursor, timeout: 1.0) ⇒ Object
Returns records with seq > cursor.
- #tail_seq ⇒ Object
Constructor Details
#initialize(capacity: 50_000) ⇒ EventLog
Returns a new instance of EventLog.
12 13 14 15 16 17 18 |
# File 'lib/vivarium/api_server.rb', line 12 def initialize(capacity: 50_000) @capacity = capacity @mutex = Mutex.new @cond = ConditionVariable.new @records = [] @seq = 0 end |
Instance Method Details
#append(bytes) ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/vivarium/api_server.rb', line 20 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.
36 37 38 39 40 41 42 43 |
# File 'lib/vivarium/api_server.rb', line 36 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_seq ⇒ Object
30 31 32 |
# File 'lib/vivarium/api_server.rb', line 30 def tail_seq @mutex.synchronize { @seq } end |