Class: Datadog::Statsd::Batch

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/statsd/batch.rb

Instance Method Summary collapse

Constructor Details

#initialize(connection, max_buffer_bytes) ⇒ Batch

Returns a new instance of Batch.



6
7
8
9
10
11
# File 'lib/datadog/statsd/batch.rb', line 6

def initialize(connection, max_buffer_bytes)
  @connection = connection
  @max_buffer_bytes = max_buffer_bytes
  @depth = 0
  reset
end

Instance Method Details

#add(message) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/datadog/statsd/batch.rb', line 26

def add(message)
  message_bytes = message.bytesize

  unless @buffer_bytes == 0
    if @buffer_bytes + 1 + message_bytes >= @max_buffer_bytes
      flush
    else
      @buffer << "\n"
      @buffer_bytes += 1
    end
  end

  @buffer << message
  @buffer_bytes += message_bytes
end

#flushObject



42
43
44
45
46
# File 'lib/datadog/statsd/batch.rb', line 42

def flush
  return if @buffer_bytes == 0
  @connection.write(@buffer)
  reset
end

#openObject



13
14
15
16
17
18
19
20
# File 'lib/datadog/statsd/batch.rb', line 13

def open
  @depth += 1

  yield
ensure
  @depth -= 1
  flush if !open?
end

#open?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/datadog/statsd/batch.rb', line 22

def open?
  @depth > 0
end