mirror of
https://github.com/discourse/discourse.git
synced 2025-05-30 15:28:37 +08:00
FIX: pool drainer to use Rails 5.2 implementation
old implementation did not reap abandoned connections
This commit is contained in:
@ -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
|
||||
|
Reference in New Issue
Block a user