Skip to content

Commit

Permalink
re-connect topology on client connect
Browse files Browse the repository at this point in the history
  • Loading branch information
HanaPearlman committed Oct 16, 2020
1 parent 09dde51 commit b214b47
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 26 deletions.
15 changes: 3 additions & 12 deletions src/mongo_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,29 +349,20 @@ 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();
cb();
return;
}

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));
});
});
}
Expand Down
21 changes: 12 additions & 9 deletions src/operations/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions test/functional/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions test/functional/sessions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
Expand All @@ -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);
});
});
}
Expand Down

0 comments on commit b214b47

Please sign in to comment.