提交 1b16e4c3 编写于 作者: R Rafael França 提交者: GitHub

Merge pull request #26988 from Paxa/connection_pool_stat

Add ActiveRecord::Base.connection_pool.stat
* Add `stat` method to `ActiveRecord::ConnectionAdapters::ConnectionPool`
Example:
ActiveRecord::Base.connection_pool.stat # =>
{ size: 15, connections: 1, busy: 1, dead: 0, idle: 0, waiting: 0, checkout_timeout: 5 }
*Pavel Evstigneev*
* Avoid `unscope(:order)` when `limit_value` is presented for `count`.
If `limit_value` is presented, records fetching order is very important
......
......@@ -581,6 +581,24 @@ def num_waiting_in_queue # :nodoc:
@available.num_waiting
end
# Return connection pool's usage statistic
# Example:
#
# ActiveRecord::Base.connection_pool.stat # => { size: 15, connections: 1, busy: 1, dead: 0, idle: 0, waiting: 0, checkout_timeout: 5 }
def stat
synchronize do
{
size: size,
connections: @connections.size,
busy: @connections.count { |c| c.in_use? && c.owner.alive? },
dead: @connections.count { |c| c.in_use? && !c.owner.alive? },
idle: @connections.count { |c| !c.in_use? },
waiting: num_waiting_in_queue,
checkout_timeout: checkout_timeout
}
end
end
private
#--
# this is unfortunately not concurrent
......
......@@ -526,6 +526,26 @@ def conn.requires_reloading? # make sure it gets removed from the pool by clear_
end
end
def test_connection_pool_stat
with_single_connection_pool do |pool|
pool.with_connection do |connection|
stats = pool.stat
assert_equal({ size: 1, connections: 1, busy: 1, dead: 0, idle: 0, waiting: 0, checkout_timeout: 5 }, stats)
end
stats = pool.stat
assert_equal({ size: 1, connections: 1, busy: 0, dead: 0, idle: 1, waiting: 0, checkout_timeout: 5 }, stats)
Thread.new do
pool.checkout
Thread.current.kill
end.join
stats = pool.stat
assert_equal({ size: 1, connections: 1, busy: 0, dead: 1, idle: 0, waiting: 0, checkout_timeout: 5 }, stats)
end
end
private
def with_single_connection_pool
one_conn_spec = ActiveRecord::Base.connection_pool.spec.dup
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册