Sha256: 1758a5fd3da9f6ececceed919bd8ab36397b6321c04662d4fb2254156bdf95e6

Contents?: true

Size: 1017 Bytes

Versions: 4

Compression:

Stored size: 1017 Bytes

Contents

require 'securerandom'

module Honeybadger
  class Agent
    class Batch
      def initialize(config, name = :data, max = 100, interval = 60, now = now)
        @id = SecureRandom.uuid
        @config = config
        @name = name
        @max = max
        @interval = interval
        @future = now + interval
        @values = Array.new
        @mutex = Mutex.new
      end

      attr_reader :id

      def push(val)
        mutex.synchronize { values.push(val) }
      end

      def empty?
        mutex.synchronize { values.empty? }
      end

      def size
        mutex.synchronize { values.size }
      end

      def flush?
        size >= max || now >= future
      end

      def as_json(*args)
        mutex.synchronize do
          { name => values.compact.map(&:to_h), :environment => config[:env], :hostname => config[:hostname] }
        end
      end

      private

      attr_reader :config, :name, :max, :values, :future, :mutex

      def now
        Time.now.to_i
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
honeybadger-2.0.0.beta.10 lib/honeybadger/agent/batch.rb
honeybadger-2.0.0.beta.9 lib/honeybadger/agent/batch.rb
honeybadger-2.0.0.beta.8 lib/honeybadger/agent/batch.rb
honeybadger-2.0.0.beta.7 lib/honeybadger/agent/batch.rb