Class: Mongo::Server::ConnectionPool
- Inherits:
-
Object
- Object
- Mongo::Server::ConnectionPool
- 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::Pool
Get a connection pool for the provided server.
Instance Method Summary collapse
-
#checkin(connection) ⇒ Object
Check a connection back into the pool.
-
#checkout ⇒ Mongo::Pool::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.
78 79 80 81 |
# File 'lib/mongo/server/connection_pool.rb', line 78 def initialize( = {}, &block) @options = .freeze @queue = Queue.new(, &block) end |
Instance Attribute Details
#options ⇒ Hash (readonly)
Returns options The pool options.
27 28 29 |
# File 'lib/mongo/server/connection_pool.rb', line 27 def @options end |
Class Method Details
.get(server) ⇒ Mongo::Pool
Get a connection pool for the provided server.
130 131 132 133 134 |
# File 'lib/mongo/server/connection_pool.rb', line 130 def get(server) ConnectionPool.new(server.) do Connection.new(server, server.) 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.
36 37 38 |
# File 'lib/mongo/server/connection_pool.rb', line 36 def checkin(connection) queue.enqueue(connection) end |
#checkout ⇒ Mongo::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.
50 51 52 |
# File 'lib/mongo/server/connection_pool.rb', line 50 def checkout queue.dequeue end |
#disconnect! ⇒ true
Disconnect the connection pool.
62 63 64 |
# File 'lib/mongo/server/connection_pool.rb', line 62 def disconnect! queue.disconnect! end |
#inspect ⇒ String
Get a pretty printed string inspection for the pool.
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_connection ⇒ Object
Yield the block to a connection, while handling checkin/checkout logic.
105 106 107 108 109 110 111 112 |
# File 'lib/mongo/server/connection_pool.rb', line 105 def with_connection begin connection = checkout yield(connection) ensure checkin(connection) if connection end end |