Class: Mongo::Server::ConnectionPool::Queue
- Inherits:
-
Object
- Object
- Mongo::Server::ConnectionPool::Queue
- Extended by:
- Forwardable
- Defined in:
- lib/mongo/server/connection_pool/queue.rb
Overview
A FIFO queue of connections to be used by the connection pool. This is based on mperham’s connection pool, implemented with a queue instead of a stack.
Constant Summary collapse
- MAX_SIZE =
The default max size for the queue.
5.freeze
- MIN_SIZE =
The default min size for the queue.
1.freeze
- WAIT_TIMEOUT =
The default timeout, in seconds, to wait for a connection.
1.freeze
Instance Attribute Summary collapse
-
#mutex ⇒ Mutex
readonly
Mutex The mutex used for synchronization.
-
#options ⇒ Hash
readonly
Options The options.
-
#queue ⇒ Array
readonly
Queue The underlying array of connections.
-
#resource ⇒ ConditionVariable
readonly
Resource The resource.
Instance Method Summary collapse
-
#dequeue ⇒ Mongo::Pool::Connection
Dequeue a connection from the queue, waiting for the provided timeout for an item if none is in the queue.
-
#disconnect! ⇒ true
Disconnect all connections in the queue.
-
#enqueue(connection) ⇒ Object
Enqueue a connection in the queue.
-
#initialize(options = {}, &block) ⇒ Queue
constructor
Initialize the new queue.
-
#inspect ⇒ String
Get a pretty printed string inspection for the queue.
-
#max_size ⇒ Integer
Get the maximum size of the queue.
-
#min_size ⇒ Integer
Get the minimum size of the queue.
-
#wait_timeout ⇒ Float
The time to wait, in seconds, for a connection to become available.
Constructor Details
#initialize(options = {}, &block) ⇒ Queue
Initialize the new queue. Will yield the block the number of times for the initial size of the queue.
110 111 112 113 114 115 116 117 |
# File 'lib/mongo/server/connection_pool/queue.rb', line 110 def initialize( = {}, &block) @block = block @connections = 0 @options = @queue = Array.new(min_size) { create_connection } @mutex = Mutex.new @resource = ConditionVariable.new end |
Instance Attribute Details
#mutex ⇒ Mutex (readonly)
Returns mutex The mutex used for synchronization.
43 44 45 |
# File 'lib/mongo/server/connection_pool/queue.rb', line 43 def mutex @mutex end |
#options ⇒ Hash (readonly)
Returns options The options.
46 47 48 |
# File 'lib/mongo/server/connection_pool/queue.rb', line 46 def @options end |
#queue ⇒ Array (readonly)
Returns queue The underlying array of connections.
40 41 42 |
# File 'lib/mongo/server/connection_pool/queue.rb', line 40 def queue @queue end |
#resource ⇒ ConditionVariable (readonly)
Returns resource The resource.
49 50 51 |
# File 'lib/mongo/server/connection_pool/queue.rb', line 49 def resource @resource end |
Instance Method Details
#dequeue ⇒ Mongo::Pool::Connection
Dequeue a connection from the queue, waiting for the provided timeout for an item if none is in the queue.
60 61 62 63 64 |
# File 'lib/mongo/server/connection_pool/queue.rb', line 60 def dequeue mutex.synchronize do dequeue_connection end end |
#disconnect! ⇒ true
Disconnect all connections in the queue.
74 75 76 77 78 79 |
# File 'lib/mongo/server/connection_pool/queue.rb', line 74 def disconnect! mutex.synchronize do queue.each{ |connection| connection.disconnect! } true end end |
#enqueue(connection) ⇒ Object
Enqueue a connection in the queue.
89 90 91 92 93 94 |
# File 'lib/mongo/server/connection_pool/queue.rb', line 89 def enqueue(connection) mutex.synchronize do queue.unshift(connection) resource.broadcast end end |
#inspect ⇒ String
Get a pretty printed string inspection for the queue.
127 128 129 130 |
# File 'lib/mongo/server/connection_pool/queue.rb', line 127 def inspect "#<Mongo::Pool::Queue:0x#{object_id} min_size=#{min_size} max_size=#{max_size} " + "wait_timeout=#{wait_timeout} current_size=#{queue.size}>" end |
#max_size ⇒ Integer
Get the maximum size of the queue.
140 141 142 |
# File 'lib/mongo/server/connection_pool/queue.rb', line 140 def max_size @max_size ||= [:max_pool_size] || MAX_SIZE end |
#min_size ⇒ Integer
Get the minimum size of the queue.
152 153 154 |
# File 'lib/mongo/server/connection_pool/queue.rb', line 152 def min_size @min_size ||= [:min_pool_size] || MIN_SIZE end |
#wait_timeout ⇒ Float
The time to wait, in seconds, for a connection to become available.
164 165 166 |
# File 'lib/mongo/server/connection_pool/queue.rb', line 164 def wait_timeout @wait_timeout ||= [:wait_queue_timeout] || WAIT_TIMEOUT end |