Ruby on Rails, Can I get the current count of connections in Mongoid pool?

1.2k Views Asked by At

I have set min_pool_size to 100 in mongoid.yml but in mongo when running db.serverStatus().connections i only get 30. Is there a way to check how many connections I have in pool?

2

There are 2 best solutions below

0
On BEST ANSWER

Setting min_pool_size in Ruby driver (and consequently Mongoid) does not mean that many network connections are actually created. Setting min_pool_size creates that many driver connection objects, but they are connected to the cluster on demand. This behavior has been fixed in version 2.11.0 of the driver - see https://jira.mongodb.org/browse/RUBY-1605.

To find out how many sockets are actually opened to a given server, first obtain its connection pool:

pool = Mongoid.default_client.cluster.next_primary.pool
# => #<Mongo::Server::ConnectionPool:0x46944310901400 queue=#<Mongo::Server::ConnectionPool::Queue:0x46944310901380 min_size=20 max_size=100 wait_timeout=1 current_size=20>>

Then look at the sockets in the connections:

pool.send(:queue).queue.map { |conn| conn.send(:socket) }.compact.count
# => 0

Flight.count

pool.send(:queue).queue.map { |conn| conn.send(:socket) }.compact.count
# => 1

Each server has one additional connection/socket opened to it for monitoring purposes which is not application-accessible.

Note that all of the code posted above (next_primary, poking into connection sockets, etc.) is not part of the driver public API and may change at any time.

0
On

connect to the admin database and run db.serverStatus():

> var status = db.serverStatus()
> status.connections
  {"current" : 21, "available" : 15979}
>