Class: SessionVision::EventBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/sessionvision/event_buffer.rb

Instance Method Summary collapse

Constructor Details

#initialize(flush_size:, flush_interval:, send_fn:, logger: nil) ⇒ EventBuffer

Returns a new instance of EventBuffer.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/sessionvision/event_buffer.rb', line 5

def initialize(flush_size:, flush_interval:, send_fn:, logger: nil)
  @flush_size = flush_size
  @flush_interval = flush_interval
  @send_fn = send_fn
  @logger = logger

  @buffer = []
  @lock = Mutex.new
  @stop = false
  @timer_thread = nil
end

Instance Method Details

#flushObject



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sessionvision/event_buffer.rb', line 36

def flush
  events = nil

  @lock.synchronize do
    return if @buffer.empty?

    events = @buffer.dup
    @buffer.clear
  end

  @logger&.debug("Flushing #{events.length} events")
  @send_fn.call(events)
end

#push(event) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/sessionvision/event_buffer.rb', line 25

def push(event)
  should_flush = false

  @lock.synchronize do
    @buffer << event
    should_flush = @buffer.length >= @flush_size
  end

  flush if should_flush
end

#shutdownObject



50
51
52
53
54
55
56
57
58
59
# File 'lib/sessionvision/event_buffer.rb', line 50

def shutdown
  @lock.synchronize { @stop = true }

  if @timer_thread
    @timer_thread.join(10)
    @timer_thread = nil
  end

  flush
end

#startObject



17
18
19
20
21
22
23
# File 'lib/sessionvision/event_buffer.rb', line 17

def start
  return if @timer_thread

  @lock.synchronize { @stop = false }
  @timer_thread = Thread.new { run_timer }
  @timer_thread.abort_on_exception = false
end