Class: BrainzLab::Flux::Buffer

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

Constant Summary collapse

MAX_EVENTS =
100
MAX_METRICS =
100
FLUSH_INTERVAL =

seconds

5

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Buffer

Returns a new instance of Buffer.



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

def initialize(client)
  @client = client
  @events = []
  @metrics = []
  @mutex = Mutex.new
  @last_flush = Time.now

  start_flush_thread
end

Instance Method Details

#add(type, data) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/brainzlab/flux/buffer.rb', line 20

def add(type, data)
  @mutex.synchronize do
    case type
    when :event
      @events << data
    when :metric
      @metrics << data
    end

    flush_if_needed
  end
end

#flush!Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/brainzlab/flux/buffer.rb', line 33

def flush!
  events_to_send = nil
  metrics_to_send = nil

  @mutex.synchronize do
    events_to_send = @events.dup
    metrics_to_send = @metrics.dup
    @events.clear
    @metrics.clear
    @last_flush = Time.now
  end

  send_batch(events_to_send, metrics_to_send)
end

#sizeObject



48
49
50
# File 'lib/brainzlab/flux/buffer.rb', line 48

def size
  @mutex.synchronize { @events.size + @metrics.size }
end