Class: Datadog::Statsd::Sender
- Inherits:
-
Object
- Object
- Datadog::Statsd::Sender
- Defined in:
- lib/datadog/statsd/sender.rb,
lib/datadog/statsd/threaded_sender.rb
Overview
Sender is using a background thread to flush and pack messages in a ‘MessageBuffer`. The communication with this thread is done using a `Queue`. If the thread is dead, it is starting a new one to avoid having a blocked Sender with no background thread to communicate with (most of the time, having a dead background thread means that a fork just happened and that we are running in the child process).
Constant Summary collapse
- CLOSEABLE_QUEUES =
Queue.instance_methods.include?(:close)
Instance Method Summary collapse
- #add(message) ⇒ Object
- #flush(sync: false) ⇒ Object
-
#initialize(message_buffer, logger: nil) ⇒ Sender
constructor
A new instance of Sender.
- #rendez_vous ⇒ Object
-
#start ⇒ Object
Compatibility with ‘Sender`.
-
#stop ⇒ Object
when calling stop, make sure that no other threads is trying to close the sender nor trying to continue to ‘#add` more message into the sender.
Constructor Details
#initialize(message_buffer, logger: nil) ⇒ Sender
Returns a new instance of Sender.
15 16 17 18 19 |
# File 'lib/datadog/statsd/sender.rb', line 15 def initialize(, logger: nil) @message_buffer = @logger = logger @mx = Mutex.new end |
Instance Method Details
#add(message) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/datadog/statsd/sender.rb', line 54 def add() raise ArgumentError, 'Start sender first' unless # if the thread does not exist, we assume we are running in a forked process, # empty the message queue and message buffers (these messages belong to # the parent process) and spawn a new companion thread. if !sender_thread.alive? @mx.synchronize { # a call from another thread has already re-created # the companion thread before this one acquired the lock break if sender_thread.alive? @logger.debug { "Statsd: companion thread is dead, re-creating one" } if @logger .close if CLOSEABLE_QUEUES @message_queue = nil .reset start } end << end |
#flush(sync: false) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/datadog/statsd/sender.rb', line 21 def flush(sync: false) # keep a copy around in case another thread is calling #stop while this method is running = # don't try to flush if there is no message_queue instantiated or # no companion thread running if ! @logger.debug { "Statsd: can't flush: no message queue ready" } if @logger return end if !sender_thread.alive? @logger.debug { "Statsd: can't flush: no sender_thread alive" } if @logger return end .push(:flush) rendez_vous if sync end |
#rendez_vous ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/datadog/statsd/sender.rb', line 40 def rendez_vous # could happen if #start hasn't be called return unless # 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 .push(queue) # wait for the sender thread to send a message # once the flush is done queue.pop end |
#start ⇒ Object
Compatibility with ‘Sender`
79 80 81 82 83 84 85 86 |
# File 'lib/datadog/statsd/threaded_sender.rb', line 79 def start raise ArgumentError, 'Sender already started' if # initialize a new message queue for the background thread @message_queue = Queue.new # start background thread @sender_thread = Thread.new(&method(:send_loop)) end |
#stop ⇒ Object
when calling stop, make sure that no other threads is trying to close the sender nor trying to continue to ‘#add` more message into the sender.
90 91 92 93 94 95 96 |
# File 'lib/datadog/statsd/sender.rb', line 90 def stop(join_worker: true) = @message_queue .close if sender_thread = @sender_thread sender_thread.join if sender_thread && join_worker end |