1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #6463 from jrochkind/connection_pool_timeout_keys

ConnectionPool wait_timeout no longer used for different types of timeouts. #6441
This commit is contained in:
Aaron Patterson 2012-05-23 09:39:09 -07:00
commit 43893f3742
4 changed files with 21 additions and 12 deletions

View file

@ -55,12 +55,20 @@ module ActiveRecord
# #
# == Options # == 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: # your database connection configuration:
# #
# * +pool+: number indicating size of connection pool (default 5) # * +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). # 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 class ConnectionPool
# Every +frequency+ seconds, the reaper will call +reap+ on +pool+. # Every +frequency+ seconds, the reaper will call +reap+ on +pool+.
# A reaper instantiated with a nil frequency will never reap the # A reaper instantiated with a nil frequency will never reap the
@ -89,7 +97,7 @@ module ActiveRecord
include MonitorMixin include MonitorMixin
attr_accessor :automatic_reconnect, :timeout attr_accessor :automatic_reconnect, :checkout_timeout, :dead_connection_timeout
attr_reader :spec, :connections, :size, :reaper attr_reader :spec, :connections, :size, :reaper
class Latch # :nodoc: class Latch # :nodoc:
@ -121,7 +129,8 @@ module ActiveRecord
# The cache of reserved connections mapped to threads # The cache of reserved connections mapped to threads
@reserved_connections = {} @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 = Reaper.new self, spec.config[:reaping_frequency]
@reaper.run @reaper.run
@ -241,7 +250,7 @@ module ActiveRecord
return checkout_and_verify(conn) if conn return checkout_and_verify(conn) if conn
end end
Timeout.timeout(@timeout, PoolFullError) { @latch.await } Timeout.timeout(@checkout_timeout, PoolFullError) { @latch.await }
end end
end end
@ -279,7 +288,7 @@ module ActiveRecord
# or a thread dies unexpectedly. # or a thread dies unexpectedly.
def reap def reap
synchronize do synchronize do
stale = Time.now - @timeout stale = Time.now - @dead_connection_timeout
connections.dup.each do |conn| connections.dup.each do |conn|
remove conn if conn.in_use? && stale > conn.last_use && !conn.active? remove conn if conn.in_use? && stale > conn.last_use && !conn.active?
end end

View file

@ -124,7 +124,7 @@ module ActiveRecord
@pool.checkout @pool.checkout
@pool.checkout @pool.checkout
@pool.checkout @pool.checkout
@pool.timeout = 0 @pool.dead_connection_timeout = 0
connections = @pool.connections.dup connections = @pool.connections.dup
@ -137,7 +137,7 @@ module ActiveRecord
@pool.checkout @pool.checkout
@pool.checkout @pool.checkout
@pool.checkout @pool.checkout
@pool.timeout = 0 @pool.dead_connection_timeout = 0
connections = @pool.connections.dup connections = @pool.connections.dup
connections.each do |conn| connections.each do |conn|

View file

@ -17,7 +17,7 @@ class PooledConnectionsTest < ActiveRecord::TestCase
end end
def checkout_connections def checkout_connections
ActiveRecord::Model.establish_connection(@connection.merge({:pool => 2, :wait_timeout => 0.3})) ActiveRecord::Model.establish_connection(@connection.merge({:pool => 2, :checkout_timeout => 0.3}))
@connections = [] @connections = []
@timed_out = 0 @timed_out = 0
@ -34,7 +34,7 @@ class PooledConnectionsTest < ActiveRecord::TestCase
# Will deadlock due to lack of Monitor timeouts in 1.9 # Will deadlock due to lack of Monitor timeouts in 1.9
def checkout_checkin_connections(pool_size, threads) def checkout_checkin_connections(pool_size, threads)
ActiveRecord::Model.establish_connection(@connection.merge({:pool => pool_size, :wait_timeout => 0.5})) ActiveRecord::Model.establish_connection(@connection.merge({:pool => pool_size, :checkout_timeout => 0.5}))
@connection_count = 0 @connection_count = 0
@timed_out = 0 @timed_out = 0
threads.times do threads.times do

View file

@ -64,7 +64,7 @@ module ActiveRecord
spec.config[:reaping_frequency] = 0.0001 spec.config[:reaping_frequency] = 0.0001
pool = ConnectionPool.new spec pool = ConnectionPool.new spec
pool.timeout = 0 pool.dead_connection_timeout = 0
conn = pool.checkout conn = pool.checkout
count = pool.connections.length count = pool.connections.length