Sha256: 39b2148085dfa1168fa860fa7b5758701780e04b25640534d750df64faae5b19

Contents?: true

Size: 1008 Bytes

Versions: 1

Compression:

Stored size: 1008 Bytes

Contents

# frozen_string_literal: true

module Datadog
  class Statsd
    class Batch
      def initialize(connection, max_buffer_bytes)
        @connection = connection
        @max_buffer_bytes = max_buffer_bytes
        @depth = 0
        reset
      end

      def open
        @depth += 1

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

      def open?
        @depth > 0
      end

      def add(message)
        message_bytes = message.bytesize

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

        @buffer << message
        @buffer_bytes += message_bytes
      end

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

      private

      def reset
        @buffer = String.new
        @buffer_bytes = 0
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dogstatsd-ruby-4.7.0 lib/datadog/statsd/batch.rb