文档菜单
文档首页
/ / /
Node.js 驱动
/ /

连接池监控

在本页面上

  • 概述
  • 事件订阅示例
  • 事件描述
  • 示例事件文档
  • connectionPoolCreated
  • connectionPoolReady
  • connectionPoolClosed
  • connectionCreated
  • connectionReady
  • connectionClosed
  • connectionCheckOutStarted
  • connectionCheckOutFailed
  • connectionCheckedOut
  • connectionCheckedIn
  • connectionPoolCleared

本指南向您展示如何监控驱动程序的连接池。连接池是驱动程序与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
当清除连接池时创建。

以下部分展示了每种连接池监控事件的示例输出。

ConnectionPoolCreatedEvent {
time: 2023-02-13T15:54:06.944Z,
address: '...',
options: {...}
}
ConnectionPoolReadyEvent {
time: 2023-02-13T15:56:38.440Z,
address: '...'
}
ConnectionPoolClosedEvent {
time: 2023-02-13T15:56:38.440Z,
address: '...'
}
ConnectionCreatedEvent {
time: 2023-02-13T15:56:38.291Z,
address: '...',
connectionId: 1
}
ConnectionReadyEvent {
time: 2023-02-13T15:56:38.291Z,
address: '...',
connectionId: 1,
durationMS: 60
}
ConnectionClosedEvent {
time: 2023-02-13T15:56:38.439Z,
address: '...',
connectionId: 1,
reason: 'poolClosed',
serviceId: undefined
}
ConnectionCheckOutStartedEvent {
time: 2023-02-13T15:56:38.291Z,
address: '...',
}
ConnectionCheckOutFailedEvent {
time: 2023-02-13T15:56:38.291Z,
address: '...',
reason: ...,
durationMS: 60
}
ConnectionCheckedOutEvent {
time: 2023-02-13T15:54:07.188Z,
address: '...',
connectionId: 1,
durationMS: 60
}
ConnectionCheckedInEvent {
time: 2023-02-13T15:54:07.189Z,
address: '...',
connectionId: 1
}
ConnectionPoolClearedEvent {
time: 2023-02-13T15:56:38.439Z,
address: '...',
serviceId: undefined,
interruptInUseConnections: true,
}

返回

命令