1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #28742 from quixoten/stack_conn_pool

Switch to LIFO for the connection pool
This commit is contained in:
Matthew Draper 2017-11-17 23:48:16 +10:30 committed by GitHub
commit eed3d3fff5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 6 deletions

View file

@ -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

View file

@ -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