Class: Mongo::Server::ConnectionPool

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Loggable
Defined in:
lib/mongo/server/connection_pool.rb,
lib/mongo/server/connection_pool/queue.rb

Overview

Represents a connection pool for server connections.

Since:

  • 2.0.0

Defined Under Namespace

Classes: Queue

Constant Summary

Constants included from Loggable

Loggable::PREFIX

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Loggable

#log_debug, #log_error, #log_fatal, #log_info, #log_warn, #logger

Constructor Details

#initialize(options = {}, &block) ⇒ ConnectionPool

Note:

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

Create the new connection pool.

Examples:

Create the new connection pool.

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

Parameters:

  • options (Hash) (defaults to: {})

    The connection pool options.

Options Hash (options):

  • :max_pool_size (Integer)

    The maximum pool size.

  • :min_pool_size (Integer)

    The minimum pool size.

  • :wait_queue_timeout (Float)

    The time to wait, in seconds, for a free connection.

Since:

  • 2.0.0



44
45
46
47
48
49
50
51
52
# 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

Instance Attribute Details

#optionsHash (readonly)

Returns options The pool options.

Returns:

  • (Hash)

    options The pool options.

Since:

  • 2.0.0



55
56
57
# File 'lib/mongo/server/connection_pool.rb', line 55

def options
  @options
end

Instance Method Details

#checkin(connection) ⇒ Object

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.

Examples:

Checkin the thread's connection to the pool.

pool.checkin

Since:

  • 2.0.0



66
67
68
# File 'lib/mongo/server/connection_pool.rb', line 66

def checkin(connection)
  queue.enqueue(connection)
end

#checkoutMongo::Server::Connection

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.

Examples:

Check a connection out from the pool.

pool.checkout

Returns:

Since:

  • 2.0.0



80
81
82
# File 'lib/mongo/server/connection_pool.rb', line 80

def checkout
  queue.dequeue
end

#disconnect!true

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.

Examples:

Disconnect the connection pool.

pool.disconnect!

Returns:

  • (true)

    true.

Since:

  • 2.1.0



95
96
97
# File 'lib/mongo/server/connection_pool.rb', line 95

def disconnect!
  queue.disconnect!
end

#inspectString

Get a pretty printed string inspection for the pool.

Examples:

Inspect the pool.

pool.inspect

Returns:

  • (String)

    The pool inspection.

Since:

  • 2.0.0



107
108
109
# File 'lib/mongo/server/connection_pool.rb', line 107

def inspect
  "#<Mongo::Server::ConnectionPool:0x#{object_id} queue=#{queue.inspect}>"
end

#with_connectionObject

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

Examples:

Execute with a connection.

pool.with_connection do |connection|
  connection.read
end

Returns:

  • (Object)

    The result of the block.

Since:

  • 2.0.0



121
122
123
124
125
126
# File 'lib/mongo/server/connection_pool.rb', line 121

def with_connection
  connection = checkout
  yield(connection)
ensure
  checkin(connection) if connection
end