mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add ActiveRecord::Base.connection_pool.stat
This commit is contained in:
parent
e91dd52d56
commit
35b6898f7c
3 changed files with 47 additions and 0 deletions
|
@ -1,3 +1,12 @@
|
||||||
|
* 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`.
|
* Avoid `unscope(:order)` when `limit_value` is presented for `count`.
|
||||||
|
|
||||||
If `limit_value` is presented, records fetching order is very important
|
If `limit_value` is presented, records fetching order is very important
|
||||||
|
|
|
@ -581,6 +581,24 @@ module ActiveRecord
|
||||||
@available.num_waiting
|
@available.num_waiting
|
||||||
end
|
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
|
private
|
||||||
#--
|
#--
|
||||||
# this is unfortunately not concurrent
|
# this is unfortunately not concurrent
|
||||||
|
|
|
@ -526,6 +526,26 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
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
|
private
|
||||||
def with_single_connection_pool
|
def with_single_connection_pool
|
||||||
one_conn_spec = ActiveRecord::Base.connection_pool.spec.dup
|
one_conn_spec = ActiveRecord::Base.connection_pool.spec.dup
|
||||||
|
|
Loading…
Reference in a new issue