class Statsd::Reporter

Attributes

batch_size[R]
messages[RW]
pool_size[R]
queue[RW]
statsd_host[R]

Public Class Methods

new(statsd_host, batch_size, pool_size) click to toggle source
# File lib/statsd/reporter.rb, line 9
def initialize(statsd_host, batch_size, pool_size)
  @pool_size   = pool_size || 1
  @statsd_host = statsd_host
  @messages    = []
  @queue       = Queue.new
  @batch_size  = batch_size
  @pool_size.times { |i| Thread.new { Thread.current[:id] = i; spawn_thread_pool } }
end

Public Instance Methods

enqueue(metric) click to toggle source
# File lib/statsd/reporter.rb, line 32
def enqueue(metric)
  queue << metric
end
spawn_thread_pool() click to toggle source
# File lib/statsd/reporter.rb, line 18
def spawn_thread_pool
  loop do
    if queue.size >= batch_size
      begin
        while messages << queue.pop(true)
          flush
        end
      rescue ThreadError
        flush #flush pending queue messages
      end
    end
  end
end

Private Instance Methods

flush() click to toggle source
# File lib/statsd/reporter.rb, line 38
def flush
  unless messages.empty?
    statsd_host.send_to_socket messages.join("\n")
    messages.clear
  end
end