aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRafael França <rafaelmfranca@gmail.com>2016-11-08 16:13:06 -0500
committerGitHub <noreply@github.com>2016-11-08 16:13:06 -0500
commit1b16e4c348055e9e7909e59e47dbe346b149ba52 (patch)
tree93b79d27fefe3965a76a9a7260a423782954ee7b
parentbab665f8936cd6a5c05e2f31b1a11f820b1d7ed9 (diff)
parent35b6898f7cb5223e1de4edc1b4539e05dfd83d41 (diff)
downloadrails-1b16e4c348055e9e7909e59e47dbe346b149ba52.tar.gz
rails-1b16e4c348055e9e7909e59e47dbe346b149ba52.tar.bz2
rails-1b16e4c348055e9e7909e59e47dbe346b149ba52.zip
Merge pull request #26988 from Paxa/connection_pool_stat
Add ActiveRecord::Base.connection_pool.stat
-rw-r--r--activerecord/CHANGELOG.md9
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb18
-rw-r--r--activerecord/test/cases/connection_pool_test.rb20
3 files changed, 47 insertions, 0 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index d2adc530a4..dd2a1b4b19 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -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`.
If `limit_value` is presented, records fetching order is very important
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 e9ecb78e27..9727d63883 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
@@ -581,6 +581,24 @@ module ActiveRecord
@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
diff --git a/activerecord/test/cases/connection_pool_test.rb b/activerecord/test/cases/connection_pool_test.rb
index 3844f1eca2..b08e4f603c 100644
--- a/activerecord/test/cases/connection_pool_test.rb
+++ b/activerecord/test/cases/connection_pool_test.rb
@@ -526,6 +526,26 @@ module ActiveRecord
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