Permalink
Browse files

connections can be reaped via the `reap` method

  • Loading branch information...
1 parent e060cf0 commit 86729eb733ce67818f079744fdea5ec63e25259a @tenderlove tenderlove committed Dec 30, 2011
@@ -232,6 +232,18 @@ def remove(conn)
end
end
+ # Removes dead connections from the pool. A dead connection can occur
+ # if a programmer forgets to close a connection at the end of a thread
+ # or a thread dies unexpectedly.
+ def reap
+ synchronize do
+ stale = Time.now - @timeout
+ connections.dup.each do |conn|
+ remove conn if conn.in_use? && stale > conn.last_use
+ end
+ end
+ end
+
private
def new_connection
@@ -25,6 +25,21 @@ def teardown
@pool.connections.each(&:close)
end
+ def test_reap
+ @pool.checkout
+ @pool.checkout
+ @pool.checkout
+ @pool.timeout = 0
+
+ connections = @pool.connections.dup
+
+ @pool.reap
+
+ assert_equal 0, @pool.connections.length
+ ensure
+ connections.map(&:close)
+ end
+
def test_remove_connection
conn = @pool.checkout
assert conn.in_use?

0 comments on commit 86729eb

Please sign in to comment.