From 1aea4debda51b0ca62270a921b4b85935780e4e6 Mon Sep 17 00:00:00 2001 From: Matt Broadstone Date: Tue, 14 Jan 2020 08:23:51 -0500 Subject: [PATCH] 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. --- lib/cmap/events.js | 14 ++++++++++++++ lib/core/sdam/server.js | 8 +++++++- lib/core/sdam/topology.js | 3 ++- lib/operations/connect.js | 16 +++++++++++----- 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/lib/cmap/events.js b/lib/cmap/events.js index e1e5e0e47b..f96021199b 100644 --- a/lib/cmap/events.js +++ b/lib/cmap/events.js @@ -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, diff --git a/lib/core/sdam/server.js b/lib/core/sdam/server.js index 7855c2a09d..0da1031b46 100644 --- a/lib/core/sdam/server.js +++ b/lib/core/sdam/server.js @@ -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(); @@ -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; }); diff --git a/lib/core/sdam/topology.js b/lib/core/sdam/topology.js index 2fbefee707..cac4777816 100644 --- a/lib/core/sdam/topology.js +++ b/lib/core/sdam/topology.js @@ -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; @@ -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([ diff --git a/lib/operations/connect.js b/lib/operations/connect.js index 57357a86f4..f649af4a51 100644 --- a/lib/operations/connect.js +++ b/lib/operations/connect.js @@ -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() { @@ -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) => {