Skip to content

Commit 1aea4de

Browse files
committed
feat: relay all CMAP events to MongoClient
The final step in introducing the new pool is to forward all of the pool events to the MongoClient, so users can listen to the events there.
1 parent ed8c9d4 commit 1aea4de

File tree

4 files changed

+34
-7
lines changed

4 files changed

+34
-7
lines changed

lib/cmap/events.js

+14
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,21 @@ class ConnectionPoolClearedEvent extends ConnectionPoolMonitoringEvent {
127127
}
128128
}
129129

130+
const CMAP_EVENT_NAMES = [
131+
'connectionPoolCreated',
132+
'connectionPoolClosed',
133+
'connectionCreated',
134+
'connectionReady',
135+
'connectionClosed',
136+
'connectionCheckOutStarted',
137+
'connectionCheckOutFailed',
138+
'connectionCheckedOut',
139+
'connectionCheckedIn',
140+
'connectionPoolCleared'
141+
];
142+
130143
module.exports = {
144+
CMAP_EVENT_NAMES,
131145
ConnectionPoolCreatedEvent,
132146
ConnectionPoolClosedEvent,
133147
ConnectionCreatedEvent,

lib/core/sdam/server.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22
const EventEmitter = require('events');
33
const ConnectionPool = require('../../cmap/connection_pool').ConnectionPool;
4+
const CMAP_EVENT_NAMES = require('../../cmap/events').CMAP_EVENT_NAMES;
45
const MongoError = require('../error').MongoError;
56
const relayEvents = require('../utils').relayEvents;
67
const BSON = require('../connection/utils').retrieveBSON();
@@ -113,7 +114,12 @@ class Server extends EventEmitter {
113114
);
114115

115116
this.s.pool = new ConnectionPool(poolOptions);
116-
relayEvents(this.s.pool, this, ['commandStarted', 'commandSucceeded', 'commandFailed']);
117+
relayEvents(
118+
this.s.pool,
119+
this,
120+
['commandStarted', 'commandSucceeded', 'commandFailed'].concat(CMAP_EVENT_NAMES)
121+
);
122+
117123
this.s.pool.on('clusterTimeReceived', clusterTime => {
118124
this.clusterTime = clusterTime;
119125
});

lib/core/sdam/topology.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const eachAsync = require('../utils').eachAsync;
2525
const emitDeprecationWarning = require('../../utils').emitDeprecationWarning;
2626
const ServerSessionPool = require('../sessions').ServerSessionPool;
2727
const makeClientMetadata = require('../utils').makeClientMetadata;
28+
const CMAP_EVENT_NAMES = require('../../cmap/events').CMAP_EVENT_NAMES;
2829

2930
const common = require('./common');
3031
const drainTimerQueue = common.drainTimerQueue;
@@ -49,7 +50,7 @@ const SERVER_RELAY_EVENTS = [
4950

5051
// NOTE: Legacy events
5152
'monitoring'
52-
];
53+
].concat(CMAP_EVENT_NAMES);
5354

5455
// all events we listen to from `Server` instances
5556
const LOCAL_SERVER_EVENTS = SERVER_RELAY_EVENTS.concat([

lib/operations/connect.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const ServerSessionPool = require('../core').Sessions.ServerSessionPool;
1818
const emitDeprecationWarning = require('../utils').emitDeprecationWarning;
1919
const fs = require('fs');
2020
const BSON = require('../core/connection/utils').retrieveBSON();
21+
const CMAP_EVENT_NAMES = require('../cmap/events').CMAP_EVENT_NAMES;
2122

2223
let client;
2324
function loadClient() {
@@ -700,23 +701,28 @@ function mergeOptions(target, source, flatten) {
700701

701702
function relayEvents(mongoClient, topology) {
702703
const serverOrCommandEvents = [
704+
// APM
705+
'commandStarted',
706+
'commandSucceeded',
707+
'commandFailed',
708+
709+
// SDAM
703710
'serverOpening',
711+
'serverClosed',
704712
'serverDescriptionChanged',
705713
'serverHeartbeatStarted',
706714
'serverHeartbeatSucceeded',
707715
'serverHeartbeatFailed',
708-
'serverClosed',
709716
'topologyOpening',
710717
'topologyClosed',
711718
'topologyDescriptionChanged',
712-
'commandStarted',
713-
'commandSucceeded',
714-
'commandFailed',
719+
720+
// Legacy
715721
'joined',
716722
'left',
717723
'ping',
718724
'ha'
719-
];
725+
].concat(CMAP_EVENT_NAMES);
720726

721727
serverOrCommandEvents.forEach(event => {
722728
topology.on(event, (object1, object2) => {

0 commit comments

Comments
 (0)