diff options
author | John Hawthorn <john@hawthorn.email> | 2019-05-17 09:44:06 -0700 |
---|---|---|
committer | John Hawthorn <john@hawthorn.email> | 2019-05-17 09:44:06 -0700 |
commit | 3e2e8eeb9ea552bd4782538cf9348455f3d0e14a (patch) | |
tree | 4a54d2ecde70bec57712ff361fb40192030756d6 | |
parent | 6f549ce53f3398403d4c2a47f9ecba910be256dd (diff) | |
download | rails-3e2e8eeb9ea552bd4782538cf9348455f3d0e14a.tar.gz rails-3e2e8eeb9ea552bd4782538cf9348455f3d0e14a.tar.bz2 rails-3e2e8eeb9ea552bd4782538cf9348455f3d0e14a.zip |
Use a single thread for all ConnectionPool Reapers
Previously we would spawn one thread per connection pool, which ends up
being wasteful for apps with several connection pools.
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb | 32 |
1 files changed, 25 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 68498b5dc5..4ff3cb0071 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb @@ -294,15 +294,33 @@ module ActiveRecord @frequency = frequency end + @@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] + Thread.new(frequency) do |t| + loop do + sleep t + @@mutex.synchronize do + @@pools[frequency].each do |p| + p.reap + p.flush + end + end + end + end + end + end + end + def run return unless frequency && frequency > 0 - Thread.new(frequency, pool) { |t, p| - loop do - sleep t - p.reap - p.flush - end - } + self.class.register_pool(pool, frequency) end end |