Sha256: 2e423e3f440fb386aa2bab55b8ff57eb7e0c6564c9722ceaae3350e3fcfbe72d

Contents?: true

Size: 1.22 KB

Versions: 11

Compression:

Stored size: 1.22 KB

Contents

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
        end
      end
    end
  end
end

Bugsnag::Delivery.register(:thread_queue, Bugsnag::Delivery::ThreadQueue)

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
bugsnag-maglev--2.8.12 lib/bugsnag/delivery/thread_queue.rb
bugsnag-2.8.12 lib/bugsnag/delivery/thread_queue.rb
bugsnag-2.8.11 lib/bugsnag/delivery/thread_queue.rb
bugsnag-2.8.10 lib/bugsnag/delivery/thread_queue.rb
bugsnag-2.8.9 lib/bugsnag/delivery/thread_queue.rb
bugsnag-2.8.8 lib/bugsnag/delivery/thread_queue.rb
bugsnag-2.8.7 lib/bugsnag/delivery/thread_queue.rb
bugsnag-2.8.6 lib/bugsnag/delivery/thread_queue.rb
bugsnag-2.8.5 lib/bugsnag/delivery/thread_queue.rb
bugsnag-2.8.4 lib/bugsnag/delivery/thread_queue.rb
bugsnag-2.8.3 lib/bugsnag/delivery/thread_queue.rb