lib/datadog/statsd/single_thread_sender.rb in dogstatsd-ruby-5.3.2 vs lib/datadog/statsd/single_thread_sender.rb in dogstatsd-ruby-5.3.3

- old
+ new

@@ -5,24 +5,28 @@ # 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. class SingleThreadSender - def initialize(message_buffer, logger: nil) + 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 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 @@ -31,15 +35,15 @@ @mx.synchronize { @message_buffer.flush() } end - # Compatibility with `Sender` def start() + @flush_timer.start if @flush_timer end - # Compatibility with `Sender` def stop() + @flush_timer.stop if @flush_timer end # Compatibility with `Sender` def rendez_vous() end