Browse files

Merge pull request #13981 from Fortisque/kevin/postgresql_reaper_thre…

…adsafe

Reaper has access to threadsafe active? call
  • Loading branch information...
2 parents f99b254 + 02a3c0e commit e4dc3035ce881baf371a8445391c9ca393406290 @tenderlove tenderlove committed Feb 8, 2014
View
2 activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
@@ -393,7 +393,7 @@ def reap
synchronize do
stale = Time.now - @dead_connection_timeout
connections.dup.each do |conn|
- if conn.in_use? && stale > conn.last_use && !conn.active?
+ if conn.in_use? && stale > conn.last_use && !conn.active_threadsafe?
remove conn
end
end
View
6 activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
@@ -262,6 +262,12 @@ def disable_referential_integrity
def active?
end
+ # Adapter should redefine this if it needs a threadsafe way to approximate
+ # if the connection is active
+ def active_threadsafe?
+ active?
+ end
+
# Disconnects from the database if already connected, and establishes a
# new connection with the database. Implementors should call super if they
# override the default implementation.
View
7 activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -586,11 +586,16 @@ def clear_cache!
# Is this connection alive and ready for queries?
def active?
- @connection.connect_poll != PG::PGRES_POLLING_FAILED
+ @connection.query 'SELECT 1'
+ true
rescue PGError
false
end
+ def active_threadsafe?
+ @connection.connect_poll != PG::PGRES_POLLING_FAILED
+ end
+
# Close then reopen the connection.
def reconnect!
super
View
2 activerecord/test/cases/connection_pool_test.rb
@@ -142,7 +142,7 @@ def test_reap_inactive
connections = @pool.connections.dup
connections.each do |conn|
- conn.extend(Module.new { def active?; false; end; })
+ conn.extend(Module.new { def active_threadsafe?; false; end; })
end
@pool.reap
View
2 activerecord/test/cases/reaper_test.rb
@@ -69,7 +69,7 @@ def test_connection_pool_starts_reaper
conn = pool.checkout
count = pool.connections.length
- conn.extend(Module.new { def active?; false; end; })
+ conn.extend(Module.new { def active_threadsafe?; false; end; })
while count == pool.connections.length
Thread.pass

0 comments on commit e4dc303

Please sign in to comment.