diff options
author | Matthew Draper <matthew@trebex.net> | 2016-07-06 08:45:54 +0930 |
---|---|---|
committer | Matthew Draper <matthew@trebex.net> | 2016-07-06 09:00:07 +0930 |
commit | f397b385c402c95b2066005b403e794fc5542868 (patch) | |
tree | a308bfecc8ed64cb91d2701f2fa95f3787bf0903 | |
parent | f4159506d68d7d7781e1534bebbf83c0c3423562 (diff) | |
download | rails-f397b385c402c95b2066005b403e794fc5542868.tar.gz rails-f397b385c402c95b2066005b403e794fc5542868.tar.bz2 rails-f397b385c402c95b2066005b403e794fc5542868.zip |
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).
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb | 17 |
1 files 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 |