提交 b72b477c 编写于 作者: A Aaron Patterson

Use connection lease to determine "checked_out" connections

上级 f866f66b
...@@ -82,7 +82,6 @@ def initialize(spec) ...@@ -82,7 +82,6 @@ def initialize(spec)
@size = (spec.config[:pool] && spec.config[:pool].to_i) || 5 @size = (spec.config[:pool] && spec.config[:pool].to_i) || 5
@connections = [] @connections = []
@checked_out = []
@automatic_reconnect = true @automatic_reconnect = true
end end
...@@ -216,20 +215,27 @@ def checkout ...@@ -216,20 +215,27 @@ def checkout
# Checkout an available connection # Checkout an available connection
@connection_mutex.synchronize do @connection_mutex.synchronize do
loop do loop do
conn = if @checked_out.size < @connections.size conn = @connections.find { |c| c.lease }
checkout_existing_connection
elsif @connections.size < @size unless conn
checkout_new_connection if @connections.size < @size
end conn = checkout_new_connection
return conn if conn conn.lease
end
end
if conn
checkout_and_verify conn
return conn
end
@queue.wait(@timeout) @queue.wait(@timeout)
if(@checked_out.size < @connections.size) if(checked_out.size < @connections.size)
next next
else else
clear_stale_cached_connections! clear_stale_cached_connections!
if @size == @checked_out.size if @size == checked_out.size
raise ConnectionTimeoutError, "could not obtain a database connection#{" within #{@timeout} seconds" if @timeout}. The max pool size is currently #{@size}; consider increasing it." raise ConnectionTimeoutError, "could not obtain a database connection#{" within #{@timeout} seconds" if @timeout}. The max pool size is currently #{@size}; consider increasing it."
end end
end end
...@@ -246,7 +252,7 @@ def checkout ...@@ -246,7 +252,7 @@ def checkout
def checkin(conn) def checkin(conn)
@connection_mutex.synchronize do @connection_mutex.synchronize do
conn.run_callbacks :checkin do conn.run_callbacks :checkin do
@checked_out.delete conn conn.expire
@queue.signal @queue.signal
end end
end end
...@@ -270,21 +276,19 @@ def checkout_new_connection ...@@ -270,21 +276,19 @@ def checkout_new_connection
c = new_connection c = new_connection
@connections << c @connections << c
checkout_and_verify(c) c
end
def checkout_existing_connection
c = (@connections - @checked_out).first
checkout_and_verify(c)
end end
def checkout_and_verify(c) def checkout_and_verify(c)
c.run_callbacks :checkout do c.run_callbacks :checkout do
c.verify! c.verify!
@checked_out << c
end end
c c
end end
def checked_out
@connections.find_all { |c| c.in_use? }
end
end end
# ConnectionHandler is a collection of ConnectionPool objects. It is used # ConnectionHandler is a collection of ConnectionPool objects. It is used
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册