Skip to content
This repository was archived by the owner on Feb 4, 2022. It is now read-only.

Commit 6984612

Browse files
committed
refactor(mongos): support passing callback to destroy
1 parent 4fd08a7 commit 6984612

File tree

1 file changed

+32
-21
lines changed

1 file changed

+32
-21
lines changed

lib/topologies/mongos.js

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,10 @@ function emitSDAMEvent(self, event, description) {
254254
}
255255

256256
const SERVER_EVENTS = ['serverDescriptionChanged', 'error', 'close', 'timeout', 'parseError'];
257-
function destroyServer(server, options) {
257+
function destroyServer(server, options, callback) {
258258
options = options || {};
259259
SERVER_EVENTS.forEach(event => server.removeAllListeners(event));
260-
server.destroy(options);
260+
server.destroy(options, callback);
261261
}
262262

263263
/**
@@ -808,32 +808,43 @@ Mongos.prototype.unref = function() {
808808
* @param {boolean} [options.force=false] Force destroy the pool
809809
* @method
810810
*/
811-
Mongos.prototype.destroy = function(options) {
812-
var self = this;
813-
// Transition state
814-
stateTransition(this, DESTROYED);
815-
// Get all proxies
816-
var proxies = this.connectedProxies.concat(this.connectingProxies);
817-
// Clear out any monitoring process
818-
if (this.haTimeoutId) clearTimeout(this.haTimeoutId);
811+
Mongos.prototype.destroy = function(options, callback) {
812+
if (this.haTimeoutId) {
813+
clearTimeout(this.haTimeoutId);
814+
}
815+
816+
const proxies = this.connectedProxies.concat(this.connectingProxies);
817+
let serverCount = proxies.length;
818+
const serverDestroyed = () => {
819+
serverCount--;
820+
if (serverCount > 0) {
821+
return;
822+
}
823+
824+
emitTopologyDescriptionChanged(this);
825+
emitSDAMEvent(this, 'topologyClosed', { topologyId: this.id });
826+
stateTransition(this, DESTROYED);
827+
if (typeof callback === 'function') {
828+
callback(null, null);
829+
}
830+
};
831+
832+
if (serverCount === 0) {
833+
serverDestroyed();
834+
return;
835+
}
819836

820837
// Destroy all connecting servers
821-
proxies.forEach(function(server) {
838+
proxies.forEach(server => {
822839
// Emit the sdam event
823-
self.emit('serverClosed', {
824-
topologyId: self.id,
840+
this.emit('serverClosed', {
841+
topologyId: this.id,
825842
address: server.name
826843
});
827844

828-
destroyServer(server, options);
829-
830-
// Move to list of disconnectedProxies
831-
moveServerFrom(self.connectedProxies, self.disconnectedProxies, server);
845+
destroyServer(server, options, serverDestroyed);
846+
moveServerFrom(this.connectedProxies, this.disconnectedProxies, server);
832847
});
833-
// Emit the final topology change
834-
emitTopologyDescriptionChanged(self);
835-
// Emit toplogy closing event
836-
emitSDAMEvent(this, 'topologyClosed', { topologyId: this.id });
837848
};
838849

839850
/**

0 commit comments

Comments
 (0)