Class: Datadog::Statsd::Sender

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

Constant Summary collapse

CLOSEABLE_QUEUES =
Queue.instance_methods.include?(:close)

Instance Method Summary collapse

Constructor Details

#initialize(message_buffer) ⇒ Sender

Returns a new instance of Sender.


8
9
10
# File 'lib/datadog/statsd/sender.rb', line 8

def initialize(message_buffer)
  @message_buffer = message_buffer
end

Instance Method Details

#add(message) ⇒ Object

Raises:

  • (ArgumentError)

32
33
34
35
36
# File 'lib/datadog/statsd/sender.rb', line 32

def add(message)
  raise ArgumentError, 'Start sender first' unless message_queue

  message_queue << message
end

#flush(sync: false) ⇒ Object


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

def flush(sync: false)
  # don't try to flush if there is no message_queue instantiated
  return unless message_queue

  message_queue.push(:flush)

  rendez_vous if sync
end

#rendez_vousObject


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

def rendez_vous
  # Initialize and get the thread's sync queue
  queue = (Thread.current[:statsd_sync_queue] ||= Queue.new)
  # tell sender-thread to notify us in the current
  # thread's queue
  message_queue.push(queue)
  # wait for the sender thread to send a message
  # once the flush is done
  queue.pop
end

#startObject

Raises:

  • (ArgumentError)

38
39
40
41
42
43
44
45
# File 'lib/datadog/statsd/sender.rb', line 38

def start
  raise ArgumentError, 'Sender already started' if message_queue

  # initialize message queue for background thread
  @message_queue = Queue.new
  # start background thread
  @sender_thread = Thread.new(&method(:send_loop))
end

#stop(join_worker: true) ⇒ Object


48
49
50
51
52
53
54
# File 'lib/datadog/statsd/sender.rb', line 48

def stop(join_worker: true)
  message_queue = @message_queue
  message_queue.close if message_queue

  sender_thread = @sender_thread
  sender_thread.join if sender_thread && join_worker
end