analytics-ruby icon indicating copy to clipboard operation
analytics-ruby copied to clipboard

Keep worker threads running until exit

Open joshuay03 opened this issue 5 months ago • 0 comments

Our (@buildkite's) Datadog profiler reported numerous threads being spawned by this gem in one of our services. We tracked it down to this return statement which causes the worker thread to terminate whenever the queue is empty: https://github.com/segmentio/analytics-ruby/blob/167f59fb0333c3a18df86923f198cf136376a3d1/lib/segment/analytics/client.rb#L173

This creates unnecessary thread churn in long-running applications. Instead of terminating when the queue is empty, the worker thread should remain alive and wait for new messages. The current behavior causes the thread to die and be replaced repeatedly - each time the queue empties and then receives new messages, a new thread must be spawned, which is inefficient, especially when analytics are emitted frequently.

This PR fixes the issue by replacing the early return with a sleep/continue pattern, keeping the worker thread alive during idle periods.

joshuay03 avatar Aug 27 '25 05:08 joshuay03