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 LIFO queue of connections to be used by the connection pool. This is based on mperham’s connection pool.
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
-
#close_stale_sockets! ⇒ Object
Close sockets that have been open for longer than the max idle time, if the option is set.
-
#dequeue ⇒ Mongo::Server::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_idle_time ⇒ Float
The maximum seconds a socket can remain idle since it has been checked in to the pool.
-
#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.
109 110 111 112 113 114 115 116 |
# File 'lib/mongo/server/connection_pool/queue.rb', line 109 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.
42 43 44 |
# File 'lib/mongo/server/connection_pool/queue.rb', line 42 def mutex @mutex end |
#options ⇒ Hash (readonly)
Returns options The options.
45 46 47 |
# File 'lib/mongo/server/connection_pool/queue.rb', line 45 def @options end |
#queue ⇒ Array (readonly)
Returns queue The underlying array of connections.
39 40 41 |
# File 'lib/mongo/server/connection_pool/queue.rb', line 39 def queue @queue end |
#resource ⇒ ConditionVariable (readonly)
Returns resource The resource.
48 49 50 |
# File 'lib/mongo/server/connection_pool/queue.rb', line 48 def resource @resource end |
Instance Method Details
#close_stale_sockets! ⇒ Object
Close sockets that have been open for longer than the max idle time, if the
option is set.
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/mongo/server/connection_pool/queue.rb', line 186 def close_stale_sockets! return unless max_idle_time to_refresh = [] queue.each do |connection| if last_checkin = connection.last_checkin if (Time.now - last_checkin) > max_idle_time to_refresh << connection end end end mutex.synchronize do num_checked_out = @connections - queue.size min_size_delta = [(min_size - num_checked_out), 0].max to_refresh.each do |connection| if queue.include?(connection) connection.disconnect! if queue.index(connection) < min_size_delta begin; connection.connect!; rescue; end end end end end end |
#dequeue ⇒ Mongo::Server::Connection
Dequeue a connection from the queue, waiting for the provided timeout for an item if none is in the queue.
59 60 61 62 63 |
# File 'lib/mongo/server/connection_pool/queue.rb', line 59 def dequeue mutex.synchronize do dequeue_connection end end |
#disconnect! ⇒ true
Disconnect all connections in the queue.
73 74 75 76 77 78 |
# File 'lib/mongo/server/connection_pool/queue.rb', line 73 def disconnect! mutex.synchronize do queue.each{ |connection| connection.disconnect! } true end end |
#enqueue(connection) ⇒ Object
Enqueue a connection in the queue.
88 89 90 91 92 93 |
# File 'lib/mongo/server/connection_pool/queue.rb', line 88 def enqueue(connection) mutex.synchronize do queue.unshift(connection.record_checkin!) resource.broadcast end end |
#inspect ⇒ String
Get a pretty printed string inspection for the queue.
126 127 128 129 |
# File 'lib/mongo/server/connection_pool/queue.rb', line 126 def inspect "#<Mongo::Server::ConnectionPool::Queue:0x#{object_id} min_size=#{min_size} max_size=#{max_size} " + "wait_timeout=#{wait_timeout} current_size=#{queue.size}>" end |
#max_idle_time ⇒ Float
The maximum seconds a socket can remain idle since it has been checked in to the pool.
175 176 177 |
# File 'lib/mongo/server/connection_pool/queue.rb', line 175 def max_idle_time @max_idle_time ||= [:max_idle_time] end |
#max_size ⇒ Integer
Get the maximum size of the queue.
139 140 141 |
# File 'lib/mongo/server/connection_pool/queue.rb', line 139 def max_size @max_size ||= [:max_pool_size] || MAX_SIZE end |
#min_size ⇒ Integer
Get the minimum size of the queue.
151 152 153 |
# File 'lib/mongo/server/connection_pool/queue.rb', line 151 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.
163 164 165 |
# File 'lib/mongo/server/connection_pool/queue.rb', line 163 def wait_timeout @wait_timeout ||= [:wait_queue_timeout] || WAIT_TIMEOUT end |