Sha256: bee701740b1ec50374137939d3338c8677fb14e0d85eb07524837ab318f969a0

Contents?: true

Size: 1.29 KB

Versions: 18

Compression:

Stored size: 1.29 KB

Contents

module Logtail
  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

18 entries across 18 versions & 2 rubygems

Version Path
logtail-ruby-0.1.3 lib/logtail/log_devices/http/flushable_dropping_sized_queue.rb
logtail-0.1.13 lib/logtail/log_devices/http/flushable_dropping_sized_queue.rb
logtail-0.1.12 lib/logtail/log_devices/http/flushable_dropping_sized_queue.rb
logtail-0.1.11 lib/logtail/log_devices/http/flushable_dropping_sized_queue.rb
logtail-0.1.10 lib/logtail/log_devices/http/flushable_dropping_sized_queue.rb
logtail-0.1.9 lib/logtail/log_devices/http/flushable_dropping_sized_queue.rb
logtail-0.1.8 lib/logtail/log_devices/http/flushable_dropping_sized_queue.rb
logtail-0.1.7 lib/logtail/log_devices/http/flushable_dropping_sized_queue.rb
logtail-0.1.6 lib/logtail/log_devices/http/flushable_dropping_sized_queue.rb
logtail-0.1.5 lib/logtail/log_devices/http/flushable_dropping_sized_queue.rb
logtail-0.1.4 lib/logtail/log_devices/http/flushable_dropping_sized_queue.rb
logtail-0.1.3 lib/logtail/log_devices/http/flushable_dropping_sized_queue.rb
logtail-0.1.2 lib/logtail/log_devices/http/flushable_dropping_sized_queue.rb
logtail-0.1.1 lib/logtail/log_devices/http/flushable_dropping_sized_queue.rb
logtail-0.1.0 lib/logtail/log_devices/http/flushable_dropping_sized_queue.rb
logtail-ruby-0.1.2 lib/logtail/log_devices/http/flushable_dropping_sized_queue.rb
logtail-ruby-0.1.1 lib/logtail/log_devices/http/flushable_dropping_sized_queue.rb
logtail-ruby-0.1.0 lib/logtail/log_devices/http/flushable_dropping_sized_queue.rb