From 981226c8d49eda6d03b1e2d3bee45b2420e40832 Mon Sep 17 00:00:00 2001 From: Hana Pearlman Date: Fri, 16 Oct 2020 15:34:41 -0400 Subject: [PATCH] re-connect topology on client connect --- src/mongo_client.ts | 16 +++------------- src/operations/connect.ts | 21 ++++++++++++--------- test/functional/connection.test.js | 6 +++--- test/functional/sessions.test.js | 4 ++-- 4 files changed, 20 insertions(+), 27 deletions(-) diff --git a/src/mongo_client.ts b/src/mongo_client.ts index 2ede2f64d2b..c5a275f8636 100644 --- a/src/mongo_client.ts +++ b/src/mongo_client.ts @@ -349,29 +349,19 @@ export class MongoClient extends EventEmitter implements OperationParent { const force = typeof forceOrCallback === 'boolean' ? forceOrCallback : false; return maybePromise(callback, cb => { - const completeClose = (err?: AnyError) => { - // clear out references to old topology - this.topology = undefined; - this.s.dbCache = new Map(); - this.s.sessions = new Set(); - - cb(err); - }; - if (this.topology == null) { - completeClose(); - return; + return cb(); } const topology = this.topology; topology.close({ force }, err => { const autoEncrypter = topology.s.options.autoEncrypter; if (!autoEncrypter) { - completeClose(err); + cb(err); return; } - autoEncrypter.teardown(force, err2 => completeClose(err || err2)); + autoEncrypter.teardown(force, err2 => cb(err || err2)); }); }); } diff --git a/src/operations/connect.ts b/src/operations/connect.ts index c99e5c81b08..0f9b89c29cf 100644 --- a/src/operations/connect.ts +++ b/src/operations/connect.ts @@ -361,18 +361,21 @@ function createTopology(mongoClient: MongoClient, options: MongoClientOptions, c options.autoEncrypter = new AutoEncrypter(mongoClient, mongoCryptOptions); } - // Create the topology - const topology = new Topology(options.servers, options); - registerDeprecatedEventNotifiers(mongoClient); + // Create the topology or re-use a topology that was previously created + if (!mongoClient.topology) { + const topology = new Topology(options.servers, options); + registerDeprecatedEventNotifiers(mongoClient); - // Add listeners - addListeners(mongoClient, topology); + // Add listeners + addListeners(mongoClient, topology); - // Propagate the events to the client - relayEvents(mongoClient, topology); + // Propagate the events to the client + relayEvents(mongoClient, topology); - // Assign the topology - mongoClient.topology = topology; + // Assign the topology + mongoClient.topology = topology; + } + const topology = mongoClient.topology; // initialize CSFLE if requested if (options.autoEncrypter) { diff --git a/test/functional/connection.test.js b/test/functional/connection.test.js index 79497eb18d7..0d92fd16785 100644 --- a/test/functional/connection.test.js +++ b/test/functional/connection.test.js @@ -280,8 +280,8 @@ describe('Connection', function () { withClient(function (client, done) { expect(client.isConnected()).to.be.true; - const collection = () => client.db('testReconnect').collection('test'); - collection().insertOne({ a: 1 }, (err, result) => { + const collection = client.db('testReconnect').collection('test'); + collection.insertOne({ a: 1 }, (err, result) => { expect(err).to.not.exist; expect(result).to.exist; @@ -293,7 +293,7 @@ describe('Connection', function () { expect(err).to.not.exist; expect(client.isConnected()).to.be.true; - collection().insertOne({ b: 2 }, (err, result) => { + collection.insertOne({ b: 2 }, (err, result) => { expect(err).to.not.exist; expect(result).to.exist; expect(client.topology.isDestroyed()).to.be.false; diff --git a/test/functional/sessions.test.js b/test/functional/sessions.test.js index eb25a46ec87..2751019bfb9 100644 --- a/test/functional/sessions.test.js +++ b/test/functional/sessions.test.js @@ -123,7 +123,7 @@ describe('Sessions', function () { // verify that the `endSessions` command was sent const lastCommand = test.commands.started[test.commands.started.length - 1]; expect(lastCommand.commandName).to.equal('endSessions'); - expect(client.topology).to.not.exist; + expect(client.topology.s.sessionPool.sessions).to.have.length(0); }); }); }); @@ -143,7 +143,7 @@ describe('Sessions', function () { // verify that the `endSessions` command was sent const lastCommand = test.commands.started[test.commands.started.length - 1]; expect(lastCommand.commandName).to.equal('endSessions'); - expect(client.topology).to.not.exist; + expect(client.topology.s.sessionPool.sessions).to.have.length(0); }); }); }