diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2019-05-27 16:31:03 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-27 16:31:03 -0400 |
commit | 1da0e25bcf2cdc5c675e7cee6d965795910c9828 (patch) | |
tree | 884ed6e856a1b54650673d01eeb163d96e9ae702 /activerecord | |
parent | b55f5a3ed9134ed86993fcda3ea9b6fb2e97f09e (diff) | |
parent | 6302e56d6c1fec048f6438d9f1ac6a8cfaed7eb9 (diff) | |
download | rails-1da0e25bcf2cdc5c675e7cee6d965795910c9828.tar.gz rails-1da0e25bcf2cdc5c675e7cee6d965795910c9828.tar.bz2 rails-1da0e25bcf2cdc5c675e7cee6d965795910c9828.zip |
Merge pull request #36345 from jhawthorn/weakref_reaper
Use WeakRef in Reaper to avoid leaking connection pools
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb | 32 |
1 files changed, 21 insertions, 11 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 4ff3cb0071..7628ef5537 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb @@ -3,6 +3,7 @@ require "thread" require "concurrent/map" require "monitor" +require "weakref" module ActiveRecord # Raised when a connection could not be obtained within the connection @@ -294,28 +295,37 @@ module ActiveRecord @frequency = frequency end - @@mutex = Mutex.new - @@pools = {} + @mutex = Mutex.new + @pools = {} - def self.register_pool(pool, frequency) # :nodoc: - @@mutex.synchronize do - if @@pools.key?(frequency) - @@pools[frequency] << pool - else - @@pools[frequency] = [pool] + class << self + def register_pool(pool, frequency) # :nodoc: + @mutex.synchronize do + unless @pools.key?(frequency) + @pools[frequency] = [] + spawn_thread(frequency) + end + @pools[frequency] << WeakRef.new(pool) + end + end + + private + + def spawn_thread(frequency) Thread.new(frequency) do |t| loop do sleep t - @@mutex.synchronize do - @@pools[frequency].each do |p| + @mutex.synchronize do + @pools[frequency].select!(&:weakref_alive?) + @pools[frequency].each do |p| p.reap p.flush + rescue WeakRef::RefError end end end end end - end end def run |