class Mongo::Server::ConnectionPool

Represents a connection pool for server connections.

@since 2.0.0

Attributes

options[R]

@return [ Hash ] options The pool options.

queue[R]

Public Class Methods

new(options = {}, &block) click to toggle source

Create the new connection pool.

@example Create the new connection pool.

Pool.new(wait_queue_timeout: 0.5) do
  Connection.new
end

@note A block must be passed to set up the connections on initialization.

@param [ Hash ] options The connection pool options.

@option options [ Integer ] :max_pool_size The maximum pool size. @option options [ Integer ] :min_pool_size The minimum pool size. @option options [ Float ] :wait_queue_timeout The time to wait, in

seconds, for a free connection.

@since 2.0.0

# File lib/mongo/server/connection_pool.rb, line 44
def initialize(options = {}, &block)
  @options = options.dup.freeze
  @queue = queue = Queue.new(@options, &block)

  finalizer = proc do
    queue.disconnect!
  end
  ObjectSpace.define_finalizer(self, finalizer)
end

Public Instance Methods

checkin(connection) click to toggle source

Check a connection back into the pool. Will pull the connection from a thread local stack that should contain it after it was checked out.

@example Checkin the thread's connection to the pool.

pool.checkin

@since 2.0.0

# File lib/mongo/server/connection_pool.rb, line 66
def checkin(connection)
  queue.enqueue(connection)
end
checkout() click to toggle source

Check a connection out from the pool. If a connection exists on the same thread it will get that connection, otherwise it will dequeue a connection from the queue and pin it to this thread.

@example Check a connection out from the pool.

pool.checkout

@return [ Mongo::Server::Connection ] The checked out connection.

@since 2.0.0

# File lib/mongo/server/connection_pool.rb, line 80
def checkout
  queue.dequeue
end
disconnect!() click to toggle source

Closes all idle connections in the pool and schedules currently checked out connections to be closed when they are checked back into the pool. The pool remains operational and can create new connections when requested.

@example Disconnect the connection pool.

pool.disconnect!

@return [ true ] true.

@since 2.1.0

# File lib/mongo/server/connection_pool.rb, line 95
def disconnect!
  queue.disconnect!
end
inspect() click to toggle source

Get a pretty printed string inspection for the pool.

@example Inspect the pool.

pool.inspect

@return [ String ] The pool inspection.

@since 2.0.0

# File lib/mongo/server/connection_pool.rb, line 107
def inspect
  "#<Mongo::Server::ConnectionPool:0x#{object_id} queue=#{queue.inspect}>"
end
with_connection() { |connection| ... } click to toggle source

Yield the block to a connection, while handling checkin/checkout logic.

@example Execute with a connection.

pool.with_connection do |connection|
  connection.read
end

@return [ Object ] The result of the block.

@since 2.0.0

# File lib/mongo/server/connection_pool.rb, line 121
def with_connection
  connection = checkout
  yield(connection)
ensure
  checkin(connection) if connection
end