mirror of
https://github.com/discourse/discourse.git
synced 2025-06-03 19:39:30 +08:00
FEATURE: added method for draining idle connections
This commit is contained in:
33
lib/freedom_patches/pool_drainer.rb
Normal file
33
lib/freedom_patches/pool_drainer.rb
Normal file
@ -0,0 +1,33 @@
|
||||
|
||||
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)
|
||||
synchronize do
|
||||
@available.clear
|
||||
@connections.delete_if do |conn|
|
||||
try_drain?(conn, idle_time)
|
||||
end
|
||||
|
||||
@connections.each do |conn|
|
||||
@available.add conn if !conn.in_use?
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def try_drain?(conn, idle_time)
|
||||
if !conn.in_use?
|
||||
if !idle_time || conn.last_use < idle_time.seconds.ago
|
||||
conn.disconnect!
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
false
|
||||
end
|
||||
|
||||
end
|
Reference in New Issue
Block a user