@@ -58,7 +58,6 @@ def push(obj, **)
5858 # @option options [Class] :exception (ConnectionPool::TimeoutError) Exception class to raise
5959 # if an entry was not available within the timeout period. Use `exception: false` to return nil.
6060 #
61- # The +timeout+ argument will be removed in 3.0.
6261 # Other options may be used by subclasses that extend TimedStack.
6362 def pop ( timeout : 0.5 , exception : ConnectionPool ::TimeoutError , **)
6463 deadline = current_time + timeout
@@ -109,7 +108,8 @@ def reap(idle_seconds:)
109108 raise ArgumentError , "idle_seconds must be a number" unless idle_seconds . is_a? ( Numeric )
110109 raise ConnectionPool ::PoolShuttingDownError if @shutdown_block
111110
112- idle . times do
111+ count = idle
112+ count . times do
113113 conn = @mutex . synchronize do
114114 raise ConnectionPool ::PoolShuttingDownError if @shutdown_block
115115 reserve_idle_connection ( idle_seconds )
@@ -197,6 +197,8 @@ def reserve_idle_connection(idle_seconds)
197197
198198 @created -= 1 unless @created == 0
199199
200+ # Most active elements are at the tail of the array.
201+ # Most idle will be at the head so `shift` rather than `pop`.
200202 @que . shift . first
201203 end
202204
@@ -205,7 +207,10 @@ def reserve_idle_connection(idle_seconds)
205207 #
206208 # Returns true if the first connection in the stack has been idle for more than idle_seconds
207209 def idle_connections? ( idle_seconds )
208- connection_stored? && ( current_time - @que . first . last > idle_seconds )
210+ return unless connection_stored?
211+ # Most idle will be at the head so `first`
212+ age = ( current_time - @que . first . last )
213+ age > idle_seconds
209214 end
210215
211216 ##
0 commit comments