Sha256: 491d55bfcc1f1c9a7596110c583d462efb63ad4b1692e594be8c6551af973824

Contents?: true

Size: 1.29 KB

Versions: 19

Compression:

Stored size: 1.29 KB

Contents

module Timber
  module LogDevices
    class HTTP
      # A simple thread-safe queue implementation that provides a #flush method.
      # The built-in ruby `Queue` class does not provide a #flush method that allows
      # the caller to retrieve all items on the queue in one call. The Ruby `SizedQueue` also
      # implements thread waiting, which is something we want to avoid. To keep things
      # simple and straight-forward, we designed this queue class.
      # @private
      class FlushableDroppingSizedQueue
        def initialize(max_size)
          @lock = Mutex.new
          @max_size = max_size
          @array = []
        end

        # Adds a message to the queue
        def enq(msg)
          @lock.synchronize do
            if !full?
              @array << msg
            end
          end
        end

        # Removes a single item from the queue
        def deq
          @lock.synchronize do
            @array.pop
          end
        end

        # Flushes all message from the queue and returns them.
        def flush
          @lock.synchronize do
            old = @array
            @array = []
            return old
          end
        end

        def full?
          size >= @max_size
        end

        def size
          @array.size
        end
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
timber-3.0.1 lib/timber/log_devices/http/flushable_dropping_sized_queue.rb
timber-3.0.0 lib/timber/log_devices/http/flushable_dropping_sized_queue.rb
timber-2.6.2 lib/timber/log_devices/http/flushable_dropping_sized_queue.rb
timber-2.6.1 lib/timber/log_devices/http/flushable_dropping_sized_queue.rb
timber-2.6.0 lib/timber/log_devices/http/flushable_dropping_sized_queue.rb
timber-2.6.0.pre.beta2 lib/timber/log_devices/http/flushable_dropping_sized_queue.rb
timber-2.6.0.pre.beta1 lib/timber/log_devices/http/flushable_dropping_sized_queue.rb
timber-2.5.1 lib/timber/log_devices/http/flushable_dropping_sized_queue.rb
timber-2.5.0 lib/timber/log_devices/http/flushable_dropping_sized_queue.rb
timber-2.4.0 lib/timber/log_devices/http/flushable_dropping_sized_queue.rb
timber-2.3.4 lib/timber/log_devices/http/flushable_dropping_sized_queue.rb
timber-2.3.3 lib/timber/log_devices/http/flushable_dropping_sized_queue.rb
timber-2.3.2 lib/timber/log_devices/http/flushable_dropping_sized_queue.rb
timber-2.3.1 lib/timber/log_devices/http/flushable_dropping_sized_queue.rb
timber-2.3.0 lib/timber/log_devices/http/flushable_dropping_sized_queue.rb
timber-2.2.3 lib/timber/log_devices/http/flushable_dropping_sized_queue.rb
timber-2.2.2 lib/timber/log_devices/http/flushable_dropping_sized_queue.rb
timber-2.2.1 lib/timber/log_devices/http/flushable_dropping_sized_queue.rb
timber-2.2.0 lib/timber/log_devices/http/flushable_dropping_sized_queue.rb