<commit_before>require "thread"

module Bugsnag
  module Delivery
    class ThreadQueue < Synchronous
      MAX_OUTSTANDING_REQUESTS = 100
      STOP = Object.new
      MUTEX = Mutex.new

      class << self
        def deliver(url, body, configuration)
          start_once!

          if queue.length > MAX_OUTSTANDING_REQUESTS
            Bugsnag.warn("Dropping notification, #{queue.length} outstanding requests")
            return
          end

          # Add delivery to the worker thread
          queue.push proc { super(url, body, configuration) }
        end

        private

        attr_reader :queue

        def start_once!
          MUTEX.synchronize do
            return if @started
            @started = true

            @queue = Queue.new

            worker_thread = Thread.new do
              while x = queue.pop
                break if x == STOP
                x.call
              end
            end

            at_exit do
              Bugsnag.warn("Waiting for #{queue.length} outstanding request(s)") unless queue.empty?
              queue.push STOP
              worker_thread.join
            end
          end<commit_msg>Don't use a private reader<commit_after>require "thread"

module Bugsnag
  module Delivery
    class ThreadQueue < Synchronous
      MAX_OUTSTANDING_REQUESTS = 100
      STOP = Object.new
      MUTEX = Mutex.new

      class << self
        def deliver(url, body, configuration)
          start_once!

          if @queue.length > MAX_OUTSTANDING_REQUESTS
            Bugsnag.warn("Dropping notification, #{@queue.length} outstanding requests")
            return
          end

          # Add delivery to the worker thread
          @queue.push proc { super(url, body, configuration) }
        end

        private

        def start_once!
          MUTEX.synchronize do
            return if @started
            @started = true

            @queue = Queue.new

            worker_thread = Thread.new do
              while x = @queue.pop
                break if x == STOP
                x.call
              end
            end

            at_exit do
              Bugsnag.warn("Waiting for #{@queue.length} outstanding request(s)") unless @queue.empty?
              @queue.push STOP
              worker_thread.join
            end
          end
{'fraction_non_alphanumeric': 0.06260794473229707, 'fraction_numerical': 0.0025906735751295338, 'mean_word_length': 1.459660297239915, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2747328', 'n_tokens_mistral': 626, 'n_tokens_neox': 585, 'n_words': 171}