连接监控/池化(Pooling)
在此页面上
此版本的文档已存档,不再提供支持。 查看最新文档,了解如何升级您的 Node.js 驱动程序版本。
Overview
本指南将向您介绍如何监控驱动程序的连接池。连接池是驱动程序与 MongoDB 实例之间维护的一组开放的 TCP 连接。连接池有助于减少应用程序需要执行的网络握手次数,还能帮助应用程序更快地运行。
以下部分演示如何在应用程序中记录连接池事件并探索这些事件中提供的信息。
事件订阅示例
您可以在应用程序中订阅一个或多个连接池事件,从而使用驱动程序访问这些事件。以下示例展示了如何连接到副本集,如何订阅 MongoDB 部署创建的其中一个连接池监控事件:
const { MongoClient } = require("mongodb"); // Replace the following with your MongoDB deployment's connection // string. const uri = "mongodb+srv://<clusterUrl>/?replicaSet=rs&writeConcern=majority"; const client = new MongoClient(uri); // Replace <event name> with the name of the event you are subscribing to. const eventName = "<event name>"; // Subscribe to the event client.on(eventName, (event) => console.log("\nreceived event:\n", event) ); async function run() { try { // Establish and verify connection await client.db("admin").command({ ping: 1 }); console.log("\nConnected successfully!\n"); } finally { // Ensures that the client will close when you finish/error await client.close(); } } run().catch(console.dir);
连接池监控事件可以帮助您调试和了解应用程序连接池的行为。 以下示例使用连接池监控事件返回池中已签出连接的计数:
function connectionPoolStatus(client) { let checkedOut = 0; function onCheckout() { checkedOut++; } function onCheckin() { checkedOut--; } function onClose() { client.removeListener('connectionCheckedOut', onCheckout); client.removeListener('connectionCheckedIn', onCheckin); checkedOut = NaN; } // Decreases count of connections checked out of the pool when connectionCheckedIn event is triggered client.on('connectionCheckedIn', onCheckin); // Increases count of connections checked out of the pool when connectionCheckedOut event is triggered client.on('connectionCheckedOut', onCheckout); // Cleans up event listeners when client is closed client.on('close', onClose); return { count: () => checkedOut, cleanUp: onClose }; }
事件描述
您可以订阅以下任何连接池监控事件:
事件名称 | 说明 |
---|---|
connectionPoolCreated | 在创建连接池时创建。 |
connectionPoolReady | 当连接池准备就绪时创建。 |
connectionPoolClosed | 在服务器实例销毁之前关闭连接池时创建。 |
connectionCreated | 在创建连接时创建,但不一定是在用于操作时创建。 |
connectionReady | 在连接成功完成一次握手并可用于操作后创建。 |
connectionClosed | 在连接关闭时创建。 |
connectionCheckOutStarted | 当操作尝试获取用于执行的连接时创建。 |
connectionCheckOutFailed | 当操作无法获取用于执行的连接时创建。 |
connectionCheckedOut | 当操作成功获取用于执行的连接时创建。 |
connectionCheckedIn | 在执行操作后,当连接被检回连接池时创建。 |
connectionPoolCleared | 当连接池被清除时创建。 |
示例事件文档
以下几节显示了每种类型的连接池监控事件的样本输出。
connectionPoolCreated
ConnectionPoolCreatedEvent { time: 2023-02-13T15:54:06.944Z, address: '...', options: {...} }
connectionPoolReady
ConnectionPoolReadyEvent { time: 2023-02-13T15:56:38.440Z, address: '...' }
connectionPoolClosed
ConnectionPoolClosedEvent { time: 2023-02-13T15:56:38.440Z, address: '...' }
connectionCreated
ConnectionCreatedEvent { time: 2023-02-13T15:56:38.291Z, address: '...', connectionId: 1 }
connectionReady
ConnectionReadyEvent { time: 2023-02-13T15:56:38.291Z, address: '...', connectionId: 1 }
connectionClosed
ConnectionClosedEvent { time: 2023-02-13T15:56:38.439Z, address: '...', connectionId: 1, reason: 'poolClosed', serviceId: undefined }
connectionCheckOutStarted
ConnectionCheckOutStartedEvent { time: 2023-02-13T15:56:38.291Z, address: '...', }
connectionCheckOutFailed
ConnectionCheckOutFailedEvent { time: 2023-02-13T15:56:38.291Z, address: '...', reason: ... }
connectionCheckedOut
ConnectionCheckedOutEvent { time: 2023-02-13T15:54:07.188Z, address: '...', connectionId: 1 }
connectionCheckedIn
ConnectionCheckedInEvent { time: 2023-02-13T15:54:07.189Z, address: '...', connectionId: 1 }
connectionPoolCleared
ConnectionPoolClearedEvent { time: 2023-02-13T15:56:38.439Z, address: '...', serviceId: undefined, interruptInUseConnections: true, }