aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorMatthew Draper <matthew@trebex.net>2017-11-17 23:48:16 +1030
committerGitHub <noreply@github.com>2017-11-17 23:48:16 +1030
commiteed3d3fff5ca6be00b2fe0fe020bd025ddbabbd5 (patch)
tree35b8ba1dcdc892570575ebe8efbdbc7e6e99b47d /activerecord
parentcd3c0fa3d04b634e272b4f5b232fdd433b3238f4 (diff)
parent47d678d9b2ab6d3e888691e8327e657ff0b5dcb0 (diff)
downloadrails-eed3d3fff5ca6be00b2fe0fe020bd025ddbabbd5.tar.gz
rails-eed3d3fff5ca6be00b2fe0fe020bd025ddbabbd5.tar.bz2
rails-eed3d3fff5ca6be00b2fe0fe020bd025ddbabbd5.zip
Merge pull request #28742 from quixoten/stack_conn_pool
Switch to LIFO for the connection pool
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb9
-rw-r--r--activerecord/test/cases/connection_pool_test.rb8
2 files changed, 11 insertions, 6 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 0759f4d2b3..6c06f67239 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
@@ -82,11 +82,8 @@ module ActiveRecord
# * private methods that require being called in a +synchronize+ blocks
# are now explicitly documented
class ConnectionPool
- # Threadsafe, fair, FIFO queue. Meant to be used by ConnectionPool
- # with which it shares a Monitor. But could be a generic Queue.
- #
- # The Queue in stdlib's 'thread' could replace this class except
- # stdlib's doesn't support waiting with a timeout.
+ # Threadsafe, fair, LIFO queue. Meant to be used by ConnectionPool
+ # with which it shares a Monitor.
class Queue
def initialize(lock = Monitor.new)
@lock = lock
@@ -175,7 +172,7 @@ module ActiveRecord
# Removes and returns the head of the queue if possible, or +nil+.
def remove
- @queue.shift
+ @queue.pop
end
# Remove and return the head the queue if the number of
diff --git a/activerecord/test/cases/connection_pool_test.rb b/activerecord/test/cases/connection_pool_test.rb
index 2bfe490602..cb2fefb4f6 100644
--- a/activerecord/test/cases/connection_pool_test.rb
+++ b/activerecord/test/cases/connection_pool_test.rb
@@ -205,6 +205,14 @@ module ActiveRecord
end.join
end
+ def test_checkout_order_is_lifo
+ conn1 = @pool.checkout
+ conn2 = @pool.checkout
+ @pool.checkin conn1
+ @pool.checkin conn2
+ assert_equal [conn2, conn1], 2.times.map { @pool.checkout }
+ end
+
# The connection pool is "fair" if threads waiting for
# connections receive them in the order in which they began
# waiting. This ensures that we don't timeout one HTTP request