class Mongo::Server::ConnectionPool
Represents a connection pool for server connections.
@since 2.0.0
Attributes
@return [ Hash ] options The pool options.
Public Class Methods
Create the new connection pool.
@example Create the new connection pool.
Pool.new(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.
@since 2.0.0
# File lib/mongo/server/connection_pool.rb, line 81 def initialize(options = {}, &block) @options = options.freeze @queue = Queue.new(options, &block) end
Private Class Methods
Get a connection pool for the provided server.
@example Get a connection pool.
Mongo::Pool.get(server)
@param [ Mongo::Server ] server The server.
@return [ Mongo::Pool ] The connection pool.
@since 2.0.0
# File lib/mongo/server/connection_pool.rb, line 133 def get(server) ConnectionPool.new(server.options) do Connection.new(server, server.options) end end
Public Instance Methods
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 39 def checkin(connection) queue.enqueue(connection) end
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::Pool::Connection ] The checked out connection.
@since 2.0.0
# File lib/mongo/server/connection_pool.rb, line 53 def checkout queue.dequeue end
Disconnect the connection pool.
@example Disconnect the connection pool.
pool.disconnect!
@return [ true ] true.
@since 2.1.0
# File lib/mongo/server/connection_pool.rb, line 65 def disconnect! queue.disconnect! end
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 94 def inspect "#<Mongo::Server::ConnectionPool:0x#{object_id} queue=#{queue.inspect}>" end
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 108 def with_connection connection = checkout yield(connection) ensure checkin(connection) if connection end