Class: ActiveRecord::Bogacs::DefaultPool
- Inherits:
-
Object
- Object
- ActiveRecord::Bogacs::DefaultPool
- Includes:
- PoolSupport, MonitorMixin
- Defined in:
- lib/active_record/bogacs/default_pool.rb
Overview
A “default” ‘ActiveRecord::ConnectionAdapters::ConnectionPool`-like pool implementation with compatibility across (older) Rails versions.
Currently, mostly, based on ActiveRecord 4.2.
api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/ConnectionPool.html
Defined Under Namespace
Classes: Queue
Instance Attribute Summary collapse
-
#automatic_reconnect ⇒ Object
Returns the value of attribute automatic_reconnect.
-
#checkout_timeout ⇒ Object
Returns the value of attribute checkout_timeout.
-
#connections ⇒ Object
readonly
Returns the value of attribute connections.
-
#initial_size ⇒ Object
readonly
Returns the value of attribute initial_size.
-
#reaper ⇒ Object
readonly
Returns the value of attribute reaper.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
-
#spec ⇒ Object
readonly
Returns the value of attribute spec.
-
#validator ⇒ Object
readonly
Returns the value of attribute validator.
Instance Method Summary collapse
-
#active_connection? ⇒ true, false
Is there an open connection that is being used for the current thread?.
-
#checkin(conn, released = nil) ⇒ Object
Check-in a database connection back into the pool.
-
#checkout ⇒ ActiveRecord::ConnectionAdapters::AbstractAdapter
Check-out a database connection from the pool, callers are expected to call #checkin when the connection is no longer needed, so that others can use it.
-
#clear_reloadable_connections! ⇒ Object
Clears the cache which maps classes.
-
#clear_stale_cached_connections! ⇒ Object
Return any checked-out connections back to the pool by threads that are no longer alive.
-
#connected? ⇒ true, false
Returns true if a connection has already been opened.
-
#connection ⇒ ActiveRecord::ConnectionAdapters::AbstractAdapter
Retrieve the connection associated with the current thread, or call #checkout to obtain one if necessary.
-
#disconnect! ⇒ Object
Disconnects all connections in the pool, and clears the pool.
-
#initialize(spec) ⇒ DefaultPool
constructor
Creates a new ‘ConnectionPool` object.
-
#logger ⇒ Object
@@logger = nil.
-
#reap ⇒ Object
Recover lost connections for the pool.
-
#reaper? ⇒ Boolean
NOTE: active? and reset! are >= AR 2.3.
- #reaping? ⇒ Boolean
-
#release_connection(with_id = current_connection_id) ⇒ Object
Signal that the thread is finished with the current connection.
-
#remove(conn) ⇒ ActiveRecord::ConnectionAdapters::AbstractAdapter
Remove a connection from the connection pool.
- #validating? ⇒ Boolean
- #validator? ⇒ Boolean
-
#verify_active_connections! ⇒ Object
Verify active connections and remove and disconnect connections associated with stale threads.
-
#with_connection {|ActiveRecord::ConnectionAdapters::AbstractAdapter| ... } ⇒ Object
If a connection already exists yield it to the block.
Methods included from PoolSupport
#current_connection_id, #new_connection
Constructor Details
#initialize(spec) ⇒ DefaultPool
The default ConnectionPool maximum size is 5.
Creates a new ‘ConnectionPool` object. spec
is a ConnectionSpecification object which describes database connection information (e.g. adapter, host name, username, password, etc), as well as the maximum size for this ConnectionPool.
wait for a connection before giving up raising a timeout (default 5 seconds). when the pool is created (default 0). run a reaper, which attempts to find and close “dead” connections (can occur if a caller forgets to close a connection at the end of a thread or a thread dies unexpectedly) default is ‘nil` - don’t run the periodical Reaper (reaping will still happen occasionally). run a connection validation (in a separate thread), to avoid potentially stale sockets when connections stay open (pooled but unused) for longer periods.
184 185 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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/active_record/bogacs/default_pool.rb', line 184 def initialize(spec) super() @spec = spec @checkout_timeout = ( spec.config[:checkout_timeout] || spec.config[:wait_timeout] || 5.0 ).to_f # <= 3.2 supports wait_timeout @reaper = Reaper.new self, spec.config[:reaping_frequency] @reaping = !! @reaper.run # default max pool size to 5 if spec.config[:pool] @size = spec.config[:pool].to_i else if defined? Rails.env && ( (! Rails.env.development? && ! Rails.env.test?) rescue nil ) logger && logger.debug("pool: option not set, using default size: 5") end @size = 5 end # The cache of reserved connections mapped to threads @reserved_connections = ThreadSafe::Map.new(:initial_capacity => @size) @connections = [] @automatic_reconnect = true @available = Queue.new self initial_size = spec.config[:pool_initial] || 0 initial_size = @size if initial_size == true initial_size = (@size * initial_size).to_i if initial_size <= 1.0 # NOTE: warn on onitial_size > size ! prefill_initial_connections if ( @initial_size = initial_size.to_i ) > 0 if frequency = spec.config[:validate_frequency] require 'active_record/bogacs/validator' unless self.class.const_defined?(:Validator) @validator = Validator.new self, frequency, spec.config[:validate_timeout] if @validator.run && @reaping logger && logger.warn(":validate_frequency configured alongside with :reaping_frequency") end end end |
Instance Attribute Details
#automatic_reconnect ⇒ Object
Returns the value of attribute automatic_reconnect.
158 159 160 |
# File 'lib/active_record/bogacs/default_pool.rb', line 158 def automatic_reconnect @automatic_reconnect end |
#checkout_timeout ⇒ Object
Returns the value of attribute checkout_timeout.
158 159 160 |
# File 'lib/active_record/bogacs/default_pool.rb', line 158 def checkout_timeout @checkout_timeout end |
#connections ⇒ Object (readonly)
Returns the value of attribute connections.
159 160 161 |
# File 'lib/active_record/bogacs/default_pool.rb', line 159 def connections @connections end |
#initial_size ⇒ Object (readonly)
Returns the value of attribute initial_size.
160 161 162 |
# File 'lib/active_record/bogacs/default_pool.rb', line 160 def initial_size @initial_size end |
#reaper ⇒ Object (readonly)
Returns the value of attribute reaper.
159 160 161 |
# File 'lib/active_record/bogacs/default_pool.rb', line 159 def reaper @reaper end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
159 160 161 |
# File 'lib/active_record/bogacs/default_pool.rb', line 159 def size @size end |
#spec ⇒ Object (readonly)
Returns the value of attribute spec.
159 160 161 |
# File 'lib/active_record/bogacs/default_pool.rb', line 159 def spec @spec end |
#validator ⇒ Object (readonly)
Returns the value of attribute validator.
159 160 161 |
# File 'lib/active_record/bogacs/default_pool.rb', line 159 def validator @validator end |
Instance Method Details
#active_connection? ⇒ true, false
Is there an open connection that is being used for the current thread?
247 248 249 250 251 252 253 254 |
# File 'lib/active_record/bogacs/default_pool.rb', line 247 def active_connection? connection_id = current_connection_id if conn = @reserved_connections.fetch(connection_id, nil) !! conn.in_use? # synchronize { conn.in_use? } else false end end |
#checkin(conn, released = nil) ⇒ Object
Check-in a database connection back into the pool.
object, which was obtained earlier by calling #checkout on this pool
367 368 369 370 371 372 373 374 375 |
# File 'lib/active_record/bogacs/default_pool.rb', line 367 def checkin(conn, released = nil) synchronize do _run_checkin_callbacks(conn) release conn, conn.owner unless released @available.add conn end end |
#checkout ⇒ ActiveRecord::ConnectionAdapters::AbstractAdapter
Check-out a database connection from the pool, callers are expected to call #checkin when the connection is no longer needed, so that others can use it.
This is done by either returning and leasing existing connection, or by creating a new connection and leasing it.
and the pool is at capacity (meaning the number of currently leased connections is greater than or equal to the size limit set)
353 354 355 356 357 358 359 360 |
# File 'lib/active_record/bogacs/default_pool.rb', line 353 def checkout conn = nil synchronize do conn = acquire_connection conn.lease end checkout_and_verify(conn) end |
#clear_reloadable_connections! ⇒ Object
Clears the cache which maps classes.
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/active_record/bogacs/default_pool.rb', line 300 def clear_reloadable_connections! synchronize do @reserved_connections.clear @connections.each do |conn| checkin conn conn.disconnect! if conn.requires_reloading? end @connections.delete_if do |conn| conn.requires_reloading? end @available.clear @connections.each do |conn| @available.add conn end end end |
#clear_stale_cached_connections! ⇒ Object
Return any checked-out connections back to the pool by threads that are no longer alive.
332 333 334 335 336 337 338 339 340 |
# File 'lib/active_record/bogacs/default_pool.rb', line 332 def clear_stale_cached_connections! keys = Thread.list.find_all { |t| t.alive? }.map(&:object_id) keys = @reserved_connections.keys - keys keys.each do |key| conn = @reserved_connections[key] checkin conn @reserved_connections.delete(key) end end |
#connected? ⇒ true, false
Returns true if a connection has already been opened.
282 283 284 |
# File 'lib/active_record/bogacs/default_pool.rb', line 282 def connected? @connections.size > 0 # synchronize { @connections.any? } end |
#connection ⇒ ActiveRecord::ConnectionAdapters::AbstractAdapter
Retrieve the connection associated with the current thread, or call #checkout to obtain one if necessary.
#connection can be called any number of times; the connection is held in a hash keyed by the thread id.
234 235 236 237 238 239 240 241 242 |
# File 'lib/active_record/bogacs/default_pool.rb', line 234 def connection connection_id = current_connection_id unless conn = @reserved_connections.fetch(connection_id, nil) synchronize do conn = ( @reserved_connections[connection_id] ||= checkout ) end end conn end |
#disconnect! ⇒ Object
Disconnects all connections in the pool, and clears the pool.
287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/active_record/bogacs/default_pool.rb', line 287 def disconnect! synchronize do @reserved_connections.clear @connections.each do |conn| checkin conn conn.disconnect! end @connections.clear @available.clear end end |
#logger ⇒ Object
@@logger = nil
422 |
# File 'lib/active_record/bogacs/default_pool.rb', line 422 def logger; ::ActiveRecord::Base.logger end |
#reap ⇒ Object
Recover lost connections for the pool. A lost connection can occur if a caller forgets to #checkin a connection for a given thread when its done or a thread dies unexpectedly.
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 |
# File 'lib/active_record/bogacs/default_pool.rb', line 395 def reap stale_connections = synchronize do @connections.select do |conn| conn.in_use? && !conn.owner.alive? end end stale_connections.each do |conn| synchronize do if conn.active? conn.reset! checkin conn else remove conn end end end end |
#reaper? ⇒ Boolean
NOTE: active? and reset! are >= AR 2.3
415 |
# File 'lib/active_record/bogacs/default_pool.rb', line 415 def reaper?; (@reaper ||= nil) && @reaper.frequency end |
#reaping? ⇒ Boolean
416 |
# File 'lib/active_record/bogacs/default_pool.rb', line 416 def reaping?; reaper? && @reaper.running? end |
#release_connection(with_id = current_connection_id) ⇒ Object
Signal that the thread is finished with the current connection. #release_connection releases the connection-thread association and returns the connection to the pool.
259 260 261 262 263 264 |
# File 'lib/active_record/bogacs/default_pool.rb', line 259 def release_connection(with_id = current_connection_id) #synchronize do conn = @reserved_connections.delete(with_id) checkin conn, true if conn #end end |
#remove(conn) ⇒ ActiveRecord::ConnectionAdapters::AbstractAdapter
Remove a connection from the connection pool. The returned connection will remain open and active but will no longer be managed by this pool.
381 382 383 384 385 386 387 388 389 390 |
# File 'lib/active_record/bogacs/default_pool.rb', line 381 def remove(conn) synchronize do @connections.delete conn @available.delete conn release conn, conn.owner @available.add checkout_new_connection if @available.any_waiting? end end |
#validating? ⇒ Boolean
419 |
# File 'lib/active_record/bogacs/default_pool.rb', line 419 def validating?; validator? && @validator.running? end |
#validator? ⇒ Boolean
418 |
# File 'lib/active_record/bogacs/default_pool.rb', line 418 def validator?; (@validator ||= nil) && @validator.frequency end |
#verify_active_connections! ⇒ Object
Verify active connections and remove and disconnect connections associated with stale threads.
320 321 322 323 324 325 326 327 |
# File 'lib/active_record/bogacs/default_pool.rb', line 320 def verify_active_connections! synchronize do clear_stale_cached_connections! @connections.each do |connection| connection.verify! end end end |
#with_connection {|ActiveRecord::ConnectionAdapters::AbstractAdapter| ... } ⇒ Object
If a connection already exists yield it to the block. If no connection exists checkout a connection, yield it to the block, and checkin the connection when finished.
271 272 273 274 275 276 277 |
# File 'lib/active_record/bogacs/default_pool.rb', line 271 def with_connection connection_id = current_connection_id fresh_connection = true unless active_connection? yield connection ensure release_connection(connection_id) if fresh_connection end |