Class: Mongo::Server::ConnectionPool

Inherits:
Object
  • Object
show all
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(timeout: 0.5) do
  Connection.new
end

Parameters:

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

    The connection pool options.

Since:

  • 2.0.0



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

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



27
28
29
# File 'lib/mongo/server/connection_pool.rb', line 27

def options
  @options
end

Class Method Details

.get(server) ⇒ Mongo::Pool

Get a connection pool for the provided server.

Examples:

Get a connection pool.

Mongo::Pool.get(server)

Parameters:

Returns:

  • (Mongo::Pool)

    The connection pool.

Since:

  • 2.0.0



130
131
132
133
134
# File 'lib/mongo/server/connection_pool.rb', line 130

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



36
37
38
# File 'lib/mongo/server/connection_pool.rb', line 36

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

#checkoutMongo::Pool::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:

  • (Mongo::Pool::Connection)

    The checked out connection.

Since:

  • 2.0.0



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

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



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

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



91
92
93
# File 'lib/mongo/server/connection_pool.rb', line 91

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



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

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