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)


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

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

  message_queue << message
end

#flush(sync: false) ⇒ Object

Raises:

  • (ArgumentError)


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

def flush(sync: false)
  raise ArgumentError, 'Start sender first' unless message_queue

  message_queue.push(:flush)

  rendez_vous if sync
end

#rendez_vousObject



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

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)


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

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



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

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