Sha256: a74fec477c38b06f7e19865d79ef6bf3ee74c0c38b4e2050b30d17a764646e8b

Contents?: true

Size: 1.42 KB

Versions: 17

Compression:

Stored size: 1.42 KB

Contents

require "thread"

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

      class << self
        ##
        # Queues a given payload to be delivered asynchronously.
        def deliver(url, body, configuration, options={})
          @configuration = configuration

          start_once!

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

          # Add delivery to the worker thread
          @queue.push proc { super(url, body, configuration, options) }
        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
              @configuration.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-6.15.0 lib/bugsnag/delivery/thread_queue.rb
bugsnag-6.14.0 lib/bugsnag/delivery/thread_queue.rb
bugsnag-6.13.1 lib/bugsnag/delivery/thread_queue.rb
bugsnag-6.13.0 lib/bugsnag/delivery/thread_queue.rb
bugsnag-6.12.2 lib/bugsnag/delivery/thread_queue.rb
bugsnag-6.12.1 lib/bugsnag/delivery/thread_queue.rb
bugsnag-6.12.0 lib/bugsnag/delivery/thread_queue.rb
bugsnag-6.11.1 lib/bugsnag/delivery/thread_queue.rb
bugsnag-6.11.0 lib/bugsnag/delivery/thread_queue.rb
bugsnag-6.10.0 lib/bugsnag/delivery/thread_queue.rb
bugsnag-6.9.0 lib/bugsnag/delivery/thread_queue.rb
bugsnag-6.8.0 lib/bugsnag/delivery/thread_queue.rb
bugsnag-6.7.3 lib/bugsnag/delivery/thread_queue.rb
bugsnag-6.7.2 lib/bugsnag/delivery/thread_queue.rb
bugsnag-6.7.1 lib/bugsnag/delivery/thread_queue.rb
bugsnag-6.7.0 lib/bugsnag/delivery/thread_queue.rb
bugsnag-6.6.4 lib/bugsnag/delivery/thread_queue.rb