Skip to content

Commit

Permalink
fix(sdam): don't emit close every time a child server closes
Browse files Browse the repository at this point in the history
The `close` event was erroneously emitted every time a child server
closed, which was not consistent with the legacy topology's
behavior. Instead, the event is now emitted when the topology itself
is closed.

NODE-2251
  • Loading branch information
mbroadst committed Nov 5, 2019
1 parent 173f292 commit 818055a
Showing 1 changed file with 14 additions and 23 deletions.
37 changes: 14 additions & 23 deletions lib/core/sdam/topology.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const resolveClusterTime = require('../topologies/shared').resolveClusterTime;
const SrvPoller = require('./srv_polling').SrvPoller;
const getMMAPError = require('../topologies/shared').getMMAPError;
const makeStateMachine = require('../utils').makeStateMachine;
const eachAsync = require('../utils').eachAsync;

// Global state
let globalTopologyCounter = 0;
Expand Down Expand Up @@ -360,31 +361,22 @@ class Topology extends EventEmitter {
// defer state transition because we may need to send an `endSessions` command above
stateTransition(this, STATE_CLOSING);

const servers = this.s.servers;
if (servers.size === 0) {
stateTransition(this, STATE_CLOSED);
if (typeof callback === 'function') {
callback(null, null);
}
eachAsync(
Array.from(this.s.servers.values()),
(server, cb) => destroyServer(server, this, options, cb),
() => {
this.s.servers.clear();

return;
}
// emit an event for close
this.emit('topologyClosed', new monitoring.TopologyClosedEvent(this.s.id));

// destroy all child servers
let destroyed = 0;
servers.forEach(server =>
destroyServer(server, this, options, () => {
destroyed++;
if (destroyed === servers.size) {
// emit an event for close
this.emit('topologyClosed', new monitoring.TopologyClosedEvent(this.s.id));

stateTransition(this, STATE_CLOSED);
if (typeof callback === 'function') {
callback(null, null);
}
stateTransition(this, STATE_CLOSED);
this.emit('close');

if (typeof callback === 'function') {
callback();
}
})
}
);
}

Expand Down Expand Up @@ -942,7 +934,6 @@ function createAndConnectServer(topology, serverDescription) {
server.once('connect', serverConnectEventHandler(server, topology));
server.on('descriptionReceived', topology.serverUpdateHandler.bind(topology));
server.on('error', serverErrorEventHandler(server, topology));
server.on('close', () => topology.emit('close', server));
server.connect();
return server;
}
Expand Down

0 comments on commit 818055a

Please sign in to comment.