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

Class Method 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
# File 'lib/mongo/server/connection_pool.rb', line 44

def initialize(options = {}, &block)
  @options = options.freeze
  @queue = Queue.new(options, &block)
end

Instance Attribute Details

#optionsHash (readonly)

Returns options The pool options.

Returns:

  • (Hash)

    options The pool options.

Since:

  • 2.0.0



50
51
52
# File 'lib/mongo/server/connection_pool.rb', line 50

def options
  @options
end

Class Method Details

.get(server) ⇒ Mongo::Server::ConnectionPool

Creates a new connection pool for the provided server.

Examples:

Create a new connection pool.

Mongo::Server::ConnectionPool.get(server)

Parameters:

Returns:

Since:

  • 2.0.0



138
139
140
141
142
# File 'lib/mongo/server/connection_pool.rb', line 138

def get(server)
  ConnectionPool.new(server.options) do |generation|
    Connection.new(server, server.options.merge(generation: generation))
  end
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



61
62
63
# File 'lib/mongo/server/connection_pool.rb', line 61

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



75
76
77
# File 'lib/mongo/server/connection_pool.rb', line 75

def checkout
  queue.dequeue
end

#disconnect!true

Disconnect the connection pool.

Examples:

Disconnect the connection pool.

pool.disconnect!

Returns:

  • (true)

    true.

Since:

  • 2.1.0



87
88
89
# File 'lib/mongo/server/connection_pool.rb', line 87

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



99
100
101
# File 'lib/mongo/server/connection_pool.rb', line 99

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



113
114
115
116
117
118
# File 'lib/mongo/server/connection_pool.rb', line 113

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