mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
each connection pool has a reaper
This commit is contained in:
parent
cde7692d4e
commit
41c24eb3e3
3 changed files with 18 additions and 2 deletions
|
@ -64,6 +64,9 @@ module ActiveRecord
|
||||||
# * +wait_timeout+: number of seconds to block and wait for a connection
|
# * +wait_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).
|
||||||
class ConnectionPool
|
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.
|
||||||
class Reaper
|
class Reaper
|
||||||
attr_reader :pool, :frequency
|
attr_reader :pool, :frequency
|
||||||
|
|
||||||
|
@ -86,7 +89,7 @@ module ActiveRecord
|
||||||
include MonitorMixin
|
include MonitorMixin
|
||||||
|
|
||||||
attr_accessor :automatic_reconnect, :timeout
|
attr_accessor :automatic_reconnect, :timeout
|
||||||
attr_reader :spec, :connections, :size
|
attr_reader :spec, :connections, :size, :reaper
|
||||||
|
|
||||||
# Creates a new ConnectionPool object. +spec+ is a ConnectionSpecification
|
# Creates a new ConnectionPool object. +spec+ is a ConnectionSpecification
|
||||||
# object which describes database connection information (e.g. adapter,
|
# object which describes database connection information (e.g. adapter,
|
||||||
|
@ -103,6 +106,7 @@ module ActiveRecord
|
||||||
@reserved_connections = {}
|
@reserved_connections = {}
|
||||||
|
|
||||||
@timeout = spec.config[:wait_timeout] || 5
|
@timeout = spec.config[:wait_timeout] || 5
|
||||||
|
@reaper = Reaper.new self, spec.config[:reaping_frequency]
|
||||||
|
|
||||||
# default max pool size to 5
|
# default max pool size to 5
|
||||||
@size = (spec.config[:pool] && spec.config[:pool].to_i) || 5
|
@size = (spec.config[:pool] && spec.config[:pool].to_i) || 5
|
||||||
|
|
|
@ -3,7 +3,7 @@ module ActiveRecord
|
||||||
class ConnectionSpecification #:nodoc:
|
class ConnectionSpecification #:nodoc:
|
||||||
attr_reader :config, :adapter_method
|
attr_reader :config, :adapter_method
|
||||||
|
|
||||||
def initialize (config, adapter_method)
|
def initialize(config, adapter_method)
|
||||||
@config, @adapter_method = config, adapter_method
|
@config, @adapter_method = config, adapter_method
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -41,6 +41,18 @@ module ActiveRecord
|
||||||
sleep 0.0002
|
sleep 0.0002
|
||||||
assert_equal(count - 1, pool.connections.length)
|
assert_equal(count - 1, pool.connections.length)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_pool_has_reaper
|
||||||
|
assert pool.reaper
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_reaping_frequency_configuration
|
||||||
|
spec = ActiveRecord::Base.connection_pool.spec
|
||||||
|
spec = ConnectionSpecification.new(spec.config.dup, spec.adapter_method)
|
||||||
|
spec.config[:reaping_frequency] = 100
|
||||||
|
pool = ConnectionPool.new spec
|
||||||
|
assert_equal 100, pool.reaper.frequency
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue