【问题标题】:Ruby on Rails, Can I get the current count of connections in Mongoid pool?Ruby on Rails,我可以获取 Mongoid 池中的当前连接数吗?
【发布时间】:2019-07-13 20:14:45
【问题描述】:

我在 mongoid.yml 中将 min_pool_size 设置为 100,但在 mongo 中运行 db.serverStatus().connections 时我只得到 30。有没有办法检查池中有多少连接?

【问题讨论】:

    标签: ruby-on-rails mongodb mongoid mongoid6


    【解决方案1】:

    在 Ruby 驱动程序(以及因此 Mongoid)中设置 min_pool_size 并不意味着实际创建了许多网络连接。设置 min_pool_size 会创建那么多驱动连接对象,但它们会按需连接到集群。此行为已在驱动程序的 2.11.0 版本中修复 - 请参阅 https://jira.mongodb.org/browse/RUBY-1605

    要找出给定服务器实际打开了多少个套接字,首先获取它的连接池:

    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>>
    

    然后查看连接中的套接字:

    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
    

    每台服务器都有一个额外的连接/套接字打开,用于监视目的,应用程序不可访问。

    请注意,上面发布的所有代码(next_primary、插入连接套接字等)都不是驱动程序公共 API 的一部分,并且可能随时更改。

    【讨论】:

      【解决方案2】:

      连接到管理数据库并运行 db.serverStatus():

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

      【讨论】:

        猜你喜欢
        • 2020-11-01
        • 2013-05-28
        • 2020-10-02
        • 2014-02-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多