Class: Datadog::Statsd::SingleThreadSender

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

Overview

The SingleThreadSender is a sender synchronously buffering messages in a ‘MessageBuffer`. It is using current Process.PID to check it is the result of a recent fork and it is reseting the MessageBuffer if that’s the case.

Instance Method Summary collapse

Constructor Details

#initialize(message_buffer, logger: nil, flush_interval: nil) ⇒ SingleThreadSender

Returns a new instance of SingleThreadSender.



10
11
12
13
14
15
16
17
18
19
# File 'lib/datadog/statsd/single_thread_sender.rb', line 10

def initialize(message_buffer, logger: nil, flush_interval: nil)
  @message_buffer = message_buffer
  @logger = logger
  @mx = Mutex.new
  if flush_interval
    @flush_timer = Datadog::Statsd::Timer.new(flush_interval) { flush }
  end
  # store the pid for which this sender has been created
  update_fork_pid
end

Instance Method Details

#add(message) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/datadog/statsd/single_thread_sender.rb', line 21

def add(message)
  @mx.synchronize {
    # we have just forked, meaning we have messages in the buffer that we should
    # not send, they belong to the parent process, let's clear the buffer.
    if forked?
      @message_buffer.reset
      @flush_timer.start if @flush_timer && @flush_timer.stop?
      update_fork_pid
    end
    @message_buffer.add(message)
  }
end

#flushObject



34
35
36
37
38
# File 'lib/datadog/statsd/single_thread_sender.rb', line 34

def flush(*)
  @mx.synchronize {
    @message_buffer.flush()
  }
end

#rendez_vousObject

Compatibility with ‘Sender`



49
50
# File 'lib/datadog/statsd/single_thread_sender.rb', line 49

def rendez_vous()
end

#startObject



40
41
42
# File 'lib/datadog/statsd/single_thread_sender.rb', line 40

def start()
  @flush_timer.start if @flush_timer
end

#stopObject



44
45
46
# File 'lib/datadog/statsd/single_thread_sender.rb', line 44

def stop()
  @flush_timer.stop if @flush_timer
end