Sha256: b023b45218cd5dc9c2b5e8d1abb15b9191a83cf93929cedfe29f26ccf985a365

Contents?: true

Size: 1.27 KB

Versions: 17

Compression:

Stored size: 1.27 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

        def start_once!
          MUTEX.synchronize do
            @started = nil unless defined?(@started)
            return if @started == Process.pid
            @started = Process.pid

            @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

17 entries across 17 versions & 1 rubygems

Version Path
bugsnag-5.5.0 lib/bugsnag/delivery/thread_queue.rb
bugsnag-5.4.1 lib/bugsnag/delivery/thread_queue.rb
bugsnag-5.4.0 lib/bugsnag/delivery/thread_queue.rb
bugsnag-5.3.3 lib/bugsnag/delivery/thread_queue.rb
bugsnag-5.3.2 lib/bugsnag/delivery/thread_queue.rb
bugsnag-5.3.1 lib/bugsnag/delivery/thread_queue.rb
bugsnag-5.3.0 lib/bugsnag/delivery/thread_queue.rb
bugsnag-5.2.0 lib/bugsnag/delivery/thread_queue.rb
bugsnag-5.1.0 lib/bugsnag/delivery/thread_queue.rb
bugsnag-5.0.1 lib/bugsnag/delivery/thread_queue.rb
bugsnag-5.0.0 lib/bugsnag/delivery/thread_queue.rb
bugsnag-4.2.1 lib/bugsnag/delivery/thread_queue.rb
bugsnag-4.2.0 lib/bugsnag/delivery/thread_queue.rb
bugsnag-4.1.0 lib/bugsnag/delivery/thread_queue.rb
bugsnag-4.0.2 lib/bugsnag/delivery/thread_queue.rb
bugsnag-4.0.1 lib/bugsnag/delivery/thread_queue.rb
bugsnag-4.0.0 lib/bugsnag/delivery/thread_queue.rb