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
19
20
# File 'lib/brainzlab/flux/buffer.rb', line 10

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

  start_flush_thread
  setup_at_exit
end

Instance Method Details

#add(type, data) ⇒ Object



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

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



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

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



50
51
52
# File 'lib/brainzlab/flux/buffer.rb', line 50

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