class ActiveRecord::ConnectionAdapters::ConnectionPool::Queue - activerecord-4.2.3 Documentation

# class ActiveRecord::ConnectionAdapters::ConnectionPool::Queue

Threadsafe, fair, FIFO queue. Meant to be used by ConnectionPool with which it shares a Monitor. But could be a generic Queue.

The Queue in stdlib's 'thread' could replace this class except stdlib's doesn't support waiting with a timeout.

### Public Class Methods

new(lock = Monitor.new) click to toggle source

    # File lib/active_record/connection_adapters/abstract/connection_pool.rb, line 74
    def initialize(lock = Monitor.new)
      @lock = lock
      @cond = @lock.new_cond
      @num_waiting = 0
      @queue = []
    end

### Public Instance Methods

add(element) click to toggle source

Add `element` to the queue. Never blocks.

    # File lib/active_record/connection_adapters/abstract/connection_pool.rb, line 97
    def add(element)
      synchronize do
        @queue.push element
        @cond.signal
      end
    end

any_waiting?() click to toggle source

Test if any threads are currently waiting on the queue.

    # File lib/active_record/connection_adapters/abstract/connection_pool.rb, line 82
    def any_waiting?
      synchronize do
        @num_waiting > 0
      end
    end

clear() click to toggle source

Remove all elements from the queue.

    # File lib/active_record/connection_adapters/abstract/connection_pool.rb, line 112
    def clear
      synchronize do
        @queue.clear
      end
    end

delete(element) click to toggle source

If `element` is in the queue, remove and return it, or nil.

    # File lib/active_record/connection_adapters/abstract/connection_pool.rb, line 105
    def delete(element)
      synchronize do
        @queue.delete(element)
      end
    end

num_waiting() click to toggle source

Returns the number of threads currently waiting on this queue.

    # File lib/active_record/connection_adapters/abstract/connection_pool.rb, line 90
    def num_waiting
      synchronize do
        @num_waiting
      end
    end

poll(timeout = nil) click to toggle source

Remove the head of the queue.

If `timeout` is not given, remove and return the head the queue if the number of available elements is strictly greater than the number of threads currently waiting (that is, don't jump ahead in line). Otherwise, return nil.

If `timeout` is given, block if it there is no element available, waiting up to `timeout` seconds for an element to become available.

Raises:

- ConnectionTimeoutError if `timeout` is given and no element

becomes available after `timeout` seconds,

    # File lib/active_record/connection_adapters/abstract/connection_pool.rb, line 132
    def poll(timeout = nil)
      synchronize do
        if timeout
          no_wait_poll || wait_poll(timeout)
        else
          no_wait_poll
        end
      end
    end

### Private Instance Methods

any?() click to toggle source

Test if the queue currently contains any elements.

    # File lib/active_record/connection_adapters/abstract/connection_pool.rb, line 149
    def any?
      !@queue.empty?
    end

can_remove_no_wait?() click to toggle source

A thread can remove an element from the queue without waiting if an only if the number of currently available connections is strictly greater than the number of waiting threads.

    # File lib/active_record/connection_adapters/abstract/connection_pool.rb, line 157
    def can_remove_no_wait?
      @queue.size > @num_waiting
    end

no_wait_poll() click to toggle source

Remove and return the head the queue if the number of available elements is strictly greater than the number of threads currently waiting. Otherwise, return nil.

    # File lib/active_record/connection_adapters/abstract/connection_pool.rb, line 169
    def no_wait_poll
      remove if can_remove_no_wait?
    end

remove() click to toggle source

Removes and returns the head of the queue if possible, or nil.

    # File lib/active_record/connection_adapters/abstract/connection_pool.rb, line 162
    def remove
      @queue.shift
    end

synchronize(&block) click to toggle source

    # File lib/active_record/connection_adapters/abstract/connection_pool.rb, line 144
    def synchronize(&block)
      @lock.synchronize(&block)
    end

wait_poll(timeout) click to toggle source

Waits on the queue up to `timeout` seconds, then removes and returns the head of the queue.

    # File lib/active_record/connection_adapters/abstract/connection_pool.rb, line 175
    def wait_poll(timeout)
      @num_waiting += 1

      t0 = Time.now
      elapsed = 0
      loop do
        @cond.wait(timeout - elapsed)

        return remove if any?

        elapsed = Time.now - t0
        if elapsed >= timeout
          msg = 'could not obtain a database connection within %0.3f seconds (waited %0.3f seconds)' %
            [timeout, elapsed]
          raise ConnectionTimeoutError, msg
        end
      end
    ensure
      @num_waiting -= 1
    end