Skip to content

Commit

Permalink
feat: relay all CMAP events to MongoClient
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mbroadst committed Jan 14, 2020
1 parent ed8c9d4 commit 1aea4de
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
14 changes: 14 additions & 0 deletions lib/cmap/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,21 @@ class ConnectionPoolClearedEvent extends ConnectionPoolMonitoringEvent {
}
}

const CMAP_EVENT_NAMES = [
'connectionPoolCreated',
'connectionPoolClosed',
'connectionCreated',
'connectionReady',
'connectionClosed',
'connectionCheckOutStarted',
'connectionCheckOutFailed',
'connectionCheckedOut',
'connectionCheckedIn',
'connectionPoolCleared'
];

module.exports = {
CMAP_EVENT_NAMES,
ConnectionPoolCreatedEvent,
ConnectionPoolClosedEvent,
ConnectionCreatedEvent,
Expand Down
8 changes: 7 additions & 1 deletion lib/core/sdam/server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
const EventEmitter = require('events');
const ConnectionPool = require('../../cmap/connection_pool').ConnectionPool;
const CMAP_EVENT_NAMES = require('../../cmap/events').CMAP_EVENT_NAMES;
const MongoError = require('../error').MongoError;
const relayEvents = require('../utils').relayEvents;
const BSON = require('../connection/utils').retrieveBSON();
Expand Down Expand Up @@ -113,7 +114,12 @@ class Server extends EventEmitter {
);

this.s.pool = new ConnectionPool(poolOptions);
relayEvents(this.s.pool, this, ['commandStarted', 'commandSucceeded', 'commandFailed']);
relayEvents(
this.s.pool,
this,
['commandStarted', 'commandSucceeded', 'commandFailed'].concat(CMAP_EVENT_NAMES)
);

this.s.pool.on('clusterTimeReceived', clusterTime => {
this.clusterTime = clusterTime;
});
Expand Down
3 changes: 2 additions & 1 deletion lib/core/sdam/topology.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const eachAsync = require('../utils').eachAsync;
const emitDeprecationWarning = require('../../utils').emitDeprecationWarning;
const ServerSessionPool = require('../sessions').ServerSessionPool;
const makeClientMetadata = require('../utils').makeClientMetadata;
const CMAP_EVENT_NAMES = require('../../cmap/events').CMAP_EVENT_NAMES;

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

// NOTE: Legacy events
'monitoring'
];
].concat(CMAP_EVENT_NAMES);

// all events we listen to from `Server` instances
const LOCAL_SERVER_EVENTS = SERVER_RELAY_EVENTS.concat([
Expand Down
16 changes: 11 additions & 5 deletions lib/operations/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const ServerSessionPool = require('../core').Sessions.ServerSessionPool;
const emitDeprecationWarning = require('../utils').emitDeprecationWarning;
const fs = require('fs');
const BSON = require('../core/connection/utils').retrieveBSON();
const CMAP_EVENT_NAMES = require('../cmap/events').CMAP_EVENT_NAMES;

let client;
function loadClient() {
Expand Down Expand Up @@ -700,23 +701,28 @@ function mergeOptions(target, source, flatten) {

function relayEvents(mongoClient, topology) {
const serverOrCommandEvents = [
// APM
'commandStarted',
'commandSucceeded',
'commandFailed',

// SDAM
'serverOpening',
'serverClosed',
'serverDescriptionChanged',
'serverHeartbeatStarted',
'serverHeartbeatSucceeded',
'serverHeartbeatFailed',
'serverClosed',
'topologyOpening',
'topologyClosed',
'topologyDescriptionChanged',
'commandStarted',
'commandSucceeded',
'commandFailed',

// Legacy
'joined',
'left',
'ping',
'ha'
];
].concat(CMAP_EVENT_NAMES);

serverOrCommandEvents.forEach(event => {
topology.on(event, (object1, object2) => {
Expand Down

0 comments on commit 1aea4de

Please sign in to comment.