Class: Mongo::Server::ConnectionPool
- Inherits:
-
Object
- Object
- Mongo::Server::ConnectionPool
- 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.
Defined Under Namespace
Classes: Queue
Constant Summary
Constants included from Loggable
Instance Attribute Summary collapse
-
#options ⇒ Hash
readonly
Options The pool options.
Class Method Summary collapse
-
.get(server) ⇒ Mongo::Server::ConnectionPool
Creates a new connection pool for the provided server.
Instance Method Summary collapse
-
#checkin(connection) ⇒ Object
Check a connection back into the pool.
-
#checkout ⇒ Mongo::Server::Connection
Check a connection out from the pool.
-
#disconnect! ⇒ true
Disconnect the connection pool.
-
#initialize(options = {}, &block) ⇒ ConnectionPool
constructor
Create the new connection pool.
-
#inspect ⇒ String
Get a pretty printed string inspection for the pool.
-
#with_connection ⇒ Object
Yield the block to a connection, while handling checkin/checkout logic.
Methods included from Loggable
#log_debug, #log_error, #log_fatal, #log_info, #log_warn, #logger
Constructor Details
#initialize(options = {}, &block) ⇒ ConnectionPool
A block must be passed to set up the connections on initialization.
Create the new connection pool.
44 45 46 47 |
# File 'lib/mongo/server/connection_pool.rb', line 44 def initialize( = {}, &block) @options = .freeze @queue = Queue.new(, &block) end |
Instance Attribute Details
#options ⇒ Hash (readonly)
Returns options The pool options.
50 51 52 |
# File 'lib/mongo/server/connection_pool.rb', line 50 def @options end |
Class Method Details
.get(server) ⇒ Mongo::Server::ConnectionPool
Creates a new connection pool for the provided server.
138 139 140 141 142 |
# File 'lib/mongo/server/connection_pool.rb', line 138 def get(server) ConnectionPool.new(server.) do |generation| Connection.new(server, server..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.
61 62 63 |
# File 'lib/mongo/server/connection_pool.rb', line 61 def checkin(connection) queue.enqueue(connection) end |
#checkout ⇒ Mongo::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.
75 76 77 |
# File 'lib/mongo/server/connection_pool.rb', line 75 def checkout queue.dequeue end |
#disconnect! ⇒ true
Disconnect the connection pool.
87 88 89 |
# File 'lib/mongo/server/connection_pool.rb', line 87 def disconnect! queue.disconnect! end |
#inspect ⇒ String
Get a pretty printed string inspection for the pool.
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_connection ⇒ Object
Yield the block to a connection, while handling checkin/checkout logic.
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 |