From 3360742396a00c9e2ff9838373788bed432d5ea7 Mon Sep 17 00:00:00 2001
From: Devin Christensen <quixoten@gmail.com>
Date: Wed, 12 Apr 2017 14:45:10 -0600
Subject: Switch to LIFO for the connection pool

Using a FIFO for the connection pool can lead to issues when there are
upstream components (pgbouncer, haproxy, etc.) that terminate
connections that are idle after a period of time. Switching to a LIFO
reduces the probability that a thread will checkout a connection that is
about to be closed by an idle timeout in an upstream component.
---
 .../lib/active_record/connection_adapters/abstract/connection_pool.rb | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

(limited to 'activerecord')

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 53dbbd8c21..ea76ff983e 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
@@ -80,7 +80,7 @@ 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
+      # Threadsafe, fair, LIFO 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
@@ -111,7 +111,7 @@ module ActiveRecord
         # Add +element+ to the queue.  Never blocks.
         def add(element)
           synchronize do
-            @queue.push element
+            @queue.unshift element
             @cond.signal
           end
         end
-- 
cgit v1.2.3