Docs HomeNode.js

Connection Pool Monitoring连接池监视

Overview概述

This guide shows you how to monitor the driver's connection pool. 本指南向您展示如何监控驱动程序的连接池A connection pool is a set of open TCP connections your driver maintains with a MongoDB instance. 连接池是驱动程序通过MongoDB实例维护的一组开放TCP连接。Connection pools help reduce the number of network handshakes your application needs to perform and can help your application run faster.连接池有助于减少应用程序需要执行的网络握手次数,并有助于应用程序更快地运行。

Read this guide if you need to record connection pool events in your application or want to explore the information provided in these events.如果您需要在应用程序中记录连接池事件,或者想了解这些事件中提供的信息,请阅读本指南。

Event Subscription Example事件订阅示例

You can access one or more connection pool events using the driver by subscribing to them in your application. 您可以通过在应用程序中订阅驱动程序来访问一个或多个连接池事件。The following example demonstrates connecting to a replica set and subscribing to one of the connection pool monitoring events created by the MongoDB deployment:以下示例演示了连接到副本集并订阅MongoDB部署创建的连接池监控事件之一:

const { MongoClient } = require("mongodb");

// Replace the following with your MongoDB deployment's connection string.将以下内容替换为MongoDB部署的连接字符串。
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.将<event name>替换为您正在订阅的事件的名称。
const eventName = "<event name>";
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);

Event Descriptions事件描述

You can subscribe to any of the following connection pool monitoring events:您可以订阅以下任何连接池监视事件:

Event Name事件名称Description描述
connectionPoolCreatedCreated when a connection pool is created.在创建连接池时创建。
connectionPoolReadyCreated when a connection pool is ready.在连接池准备就绪时创建。
connectionPoolClosedCreated when a connection pool is closed, prior to server instance destruction.在服务器实例销毁之前关闭连接池时创建。
connectionCreatedCreated when a connection is created, but not necessarily when it is used for an operation.在创建连接时创建,但不一定在用于操作时创建。
connectionReadyCreated after a connection has successfully completed a handshake and is ready to be used for operations.在连接成功完成握手并准备用于操作之后创建。
connectionClosedCreated when a connection is closed.在连接关闭时创建。
connectionCheckOutStartedCreated when an operation attempts to acquire a connection for execution.当操作尝试获取连接以供执行时创建。
connectionCheckOutFailedCreated when an operation fails to acquire a connection for execution.当操作无法获取要执行的连接时创建。
connectionCheckedOutCreated when an operation successfully acquires a connection for execution.在操作成功获取要执行的连接时创建。
connectionCheckedInCreated when a connection is checked back into the pool after an operation is executed.在执行操作后将连接签回池时创建。
connectionPoolClearedCreated when a connection pool is cleared.在清除连接池时创建。

Example Event Documents事件文档示例

The following sections show sample output for each type of connection pool monitoring event.以下部分显示了每种类型的连接池监视事件的示例输出。

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