Sha256: 59b1456908d1da15ba1441852aad02e06770bbe79bf27f3e6ad2da23b6db14df

Contents?: true

Size: 722 Bytes

Versions: 1

Compression:

Stored size: 722 Bytes

Contents

require 'thread'
require 'timeout'

module EasyBunnyRPC
  class TimedQueue
    def initialize
      @array = Array.new
      @mutex = Mutex.new
      @cv = ConditionVariable.new
    end

    def push(item)
      @mutex.synchronize do
        @array.push(item)
        @cv.signal
      end
    end

    def pop_with_timeout(timeout=0.5)
      timeout_at = Time.now + timeout

      @mutex.synchronize do
        loop do
          if @array.empty?
            remaining = timeout_at - Time.now

            raise(Timeout::Error, "Waited #{timeout} seconds to pop") if(remaining <= 0)

            @cv.wait(@mutex, remaining)
          else
            return @array.pop
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
easy_bunny_rpc-0.1.0.alpha lib/easy_bunny_rpc/timed_queue.rb