개요
이 가이드에서는 드라이버의 연결 풀을 모니터링하는 방법을 보여 줍니다. 연결 풀은 드라이버가 MongoDB 인스턴스를 사용해 유지 관리하는 개방형 TCP 연결 집합입니다. 연결 풀은 애플리케이션이 수행해야 하는 네트워크 핸드셰이크 수를 줄이고 애플리케이션을 더 빠르게 실행하는 데 도움이 될 수 있습니다.
다음 섹션에서는 애플리케이션에서 연결 풀 이벤트를 기록하고 이러한 이벤트에서 제공된 정보를 탐색하는 방법을 보여 줍니다.
이벤트 구독 예시
애플리케이션에서 드라이버를 구독하여 하나 이상의 연결 풀 이벤트에 액세스할 수 있습니다. 다음 예시에서는 복제본 세트에 연결하고 MongoDB deployment에 의해 생성된 연결 풀 모니터링 이벤트 중 하나를 구독하는 방법을 보여 줍니다.
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
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,   durationMS: 60 } 
연결종료
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: ...,   durationMS: 60 } 
connectionCheckedOut
ConnectionCheckedOutEvent {   time: 2023-02-13T15:54:07.188Z,   address: '...',   connectionId: 1,   durationMS: 60 } 
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, }