From f397b385c402c95b2066005b403e794fc5542868 Mon Sep 17 00:00:00 2001 From: Matthew Draper <matthew@trebex.net> Date: Wed, 6 Jul 2016 08:45:54 +0930 Subject: Reduce locking by taking ownership of stale connections This way, we aren't racing other threads, so we don't need to re-check the conditional. And we no longer need to hold the lock while calling remove (which can choose to make a new connection while we wait). --- .../connection_adapters/abstract/connection_pool.rb | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 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 51bbc02b0c..98cf97910e 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb @@ -556,19 +556,18 @@ module ActiveRecord stale_connections = synchronize do @connections.select do |conn| conn.in_use? && !conn.owner.alive? + end.each do |conn| + conn.expire + conn.lease end end stale_connections.each do |conn| - synchronize do - next unless conn.in_use? && !conn.owner.alive? - - if conn.active? - conn.reset! - checkin conn - else - remove conn - end + if conn.active? + conn.reset! + checkin conn + else + remove conn end end end -- cgit v1.2.3