Class: BrainzLab::Recall::Buffer

Inherits:
Object
  • Object
show all
Defined in:
lib/brainzlab/recall/buffer.rb

Instance Method Summary collapse

Constructor Details

#initialize(config, client) ⇒ Buffer

Returns a new instance of Buffer.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/brainzlab/recall/buffer.rb', line 8

def initialize(config, client)
  @config = config
  @client = client
  @buffer = Concurrent::Array.new
  @mutex = Mutex.new
  @flush_thread = nil
  @shutdown = false

  start_flush_thread
  setup_at_exit
end

Instance Method Details

#flushObject



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/brainzlab/recall/buffer.rb', line 27

def flush
  return if @buffer.empty?

  entries = nil
  @mutex.synchronize do
    entries = @buffer.dup
    @buffer.clear
  end

  return if entries.nil? || entries.empty?

  @client.send_batch(entries)
end

#push(log_entry) ⇒ Object



20
21
22
23
24
25
# File 'lib/brainzlab/recall/buffer.rb', line 20

def push(log_entry)
  @buffer.push(log_entry)
  # Skip synchronous flush during instrumentation to avoid blocking the host app.
  # The background flush thread will send these entries within recall_flush_interval seconds.
  flush if @buffer.size >= @config.recall_buffer_size && !BrainzLab.instrumenting?
end

#shutdownObject



41
42
43
44
45
# File 'lib/brainzlab/recall/buffer.rb', line 41

def shutdown
  @shutdown = true
  @flush_thread&.kill
  flush
end