mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #36296 from jhawthorn/dont_fear_the_reaper
Use a single thread for all ConnectionPool Reapers
This commit is contained in:
commit
1f89d432c5
1 changed files with 25 additions and 7 deletions
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue