aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
diff options
context:
space:
mode:
authorJonathan Rochkind <jonathan@dnil.net>2012-05-23 12:08:11 -0400
committerJonathan Rochkind <jonathan@dnil.net>2012-05-23 12:08:11 -0400
commitcb6f839359bd894feb0a1105b79af72b336aa41e (patch)
treedc0683e9c13633ab79ef9fd9a24e150954bd6ecf /activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
parentc1487f619045840d89f1e2dbc6e133e1c2cbf2f4 (diff)
downloadrails-cb6f839359bd894feb0a1105b79af72b336aa41e.tar.gz
rails-cb6f839359bd894feb0a1105b79af72b336aa41e.tar.bz2
rails-cb6f839359bd894feb0a1105b79af72b336aa41e.zip
ConnectionPool wait_timeout no longer used for different types of timeouts. #6441
An AR ConnectionSpec `wait_timeout` is pre-patch used for three different things: * mysql2 uses it for MySQL's own wait_timeout (how long MySQL should allow an idle connection before closing it), and defaults to 2592000 seconds. * ConnectionPool uses it for "number of seconds to block and wait for a connection before giving up and raising a timeout error", default 5 seconds. * ConnectionPool uses it for the Reaper, for deciding if a 'dead' connection can be reaped. Default 5 seconds. Previously, if you want to change these from defaults, you need to change them all together. This is problematic _especially_ for the mysql2/ConnectionPool conflict, you will generally _not_ want them to be the same, as evidenced by their wildly different defaults. This has caused real problems for people #6441 #2894 But as long as we're changing this, forcing renaming the ConnectionPool key to be more specific, it made sense to seperate the two ConnectionPool uses too -- these two types of ConnectionPool timeouts ought to be able to be changed independently, you won't neccesarily want them to be the same, even though the defaults are (currently) the same.
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb')
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb23
1 files changed, 16 insertions, 7 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
index c6699737b4..be36c9695f 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
@@ -55,19 +55,27 @@ module ActiveRecord
#
# == Options
#
- # There are two connection-pooling-related options that you can add to
+ # There are several connection-pooling-related options that you can add to
# your database connection configuration:
#
# * +pool+: number indicating size of connection pool (default 5)
- # * +wait_timeout+: number of seconds to block and wait for a connection
+ # * +checkout_timeout+: number of seconds to block and wait for a connection
# before giving up and raising a timeout error (default 5 seconds).
+ # * +reaping_frequency+: frequency in seconds to periodically run the
+ # Reaper, which attempts to find and close dead connections, which can
+ # occur if a programmer forgets to close a connection at the end of a
+ # thread or a thread dies unexpectedly. (Default nil, which means don't
+ # run the Reaper).
+ # * +dead_connection_timeout+: number of seconds from last checkout
+ # after which the Reaper will consider a connection reapable. (default
+ # 5 seconds).
class ConnectionPool
# Every +frequency+ seconds, the reaper will call +reap+ on +pool+.
# A reaper instantiated with a nil frequency will never reap the
# connection pool.
#
# Configure the frequency by setting "reaping_frequency" in your
- # database yaml file.
+ # database yaml file.
class Reaper
attr_reader :pool, :frequency
@@ -89,7 +97,7 @@ module ActiveRecord
include MonitorMixin
- attr_accessor :automatic_reconnect, :timeout
+ attr_accessor :automatic_reconnect, :checkout_timeout, :dead_connection_timeout
attr_reader :spec, :connections, :size, :reaper
class Latch # :nodoc:
@@ -121,7 +129,8 @@ module ActiveRecord
# The cache of reserved connections mapped to threads
@reserved_connections = {}
- @timeout = spec.config[:wait_timeout] || 5
+ @checkout_timeout = spec.config[:checkout_timeout] || 5
+ @dead_connection_timeout = spec.config[:dead_connection_timeout]
@reaper = Reaper.new self, spec.config[:reaping_frequency]
@reaper.run
@@ -241,7 +250,7 @@ module ActiveRecord
return checkout_and_verify(conn) if conn
end
- Timeout.timeout(@timeout, PoolFullError) { @latch.await }
+ Timeout.timeout(@checkout_timeout, PoolFullError) { @latch.await }
end
end
@@ -279,7 +288,7 @@ module ActiveRecord
# or a thread dies unexpectedly.
def reap
synchronize do
- stale = Time.now - @timeout
+ stale = Time.now - @dead_connection_timeout
connections.dup.each do |conn|
remove conn if conn.in_use? && stale > conn.last_use && !conn.active?
end