FIX: pool drainer to use Rails 5.2 implementation

old implementation did not reap abandoned connections
This commit is contained in:
Sam
2018-06-14 15:54:48 +10:00
parent 8fc08aad09
commit 71aa20bd30
3 changed files with 23 additions and 23 deletions

View File

@ -25,31 +25,23 @@ class ActiveRecord::ConnectionAdapters::ConnectionPool
# drain all idle connections
# if idle_time is specified only connections idle for N seconds will be drained
def drain(idle_time = nil, max_age = nil)
synchronize do
if @available && @connections
@available.clear
@connections.delete_if do |conn|
try_drain?(conn, idle_time, max_age)
end
return if !(@connections && @available)
@connections.each do |conn|
@available.add conn if !conn.in_use?
end
idle_connections = synchronize do
@connections.select do |conn|
!conn.in_use? && ((idle_time && conn.last_use <= idle_time.seconds.ago) || (max_age && conn.first_use < max_age.seconds.ago))
end.each do |conn|
conn.lease
@available.delete conn
@connections.delete conn
end
end
idle_connections.each do |conn|
conn.disconnect!
end
end
private
def try_drain?(conn, idle_time, max_age)
if !conn.in_use?
if !idle_time || conn.last_use < idle_time.seconds.ago || (max_age && conn.first_use < max_age.seconds.ago)
conn.disconnect!
return true
end
end
false
end
end