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

Commit bc1611d

Browse files
committed
refactor(replset): support passing callback to destroy
1 parent 6984612 commit bc1611d

File tree

2 files changed

+69
-37
lines changed

2 files changed

+69
-37
lines changed

lib/topologies/replset.js

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -948,19 +948,29 @@ ReplSet.prototype.auth = function(credentials, callback) {
948948
* @param {boolean} [options.force=false] Force destroy the pool
949949
* @method
950950
*/
951-
ReplSet.prototype.destroy = function(options) {
951+
ReplSet.prototype.destroy = function(options, callback) {
952952
options = options || {};
953-
// Transition state
954-
stateTransition(this, DESTROYED);
953+
954+
let destroyCount = this.s.connectingServers.length + 1; // +1 for the callback from `replicaSetState.destroy`
955+
const serverDestroyed = () => {
956+
destroyCount--;
957+
if (destroyCount > 0) {
958+
return;
959+
}
960+
961+
// Emit toplogy closing event
962+
emitSDAMEvent(this, 'topologyClosed', { topologyId: this.id });
963+
964+
// Transition state
965+
stateTransition(this, DESTROYED);
966+
967+
if (typeof callback === 'function') {
968+
callback(null, null);
969+
}
970+
};
971+
955972
// Clear out any monitoring process
956973
if (this.haTimeoutId) clearTimeout(this.haTimeoutId);
957-
// Destroy the replicaset
958-
this.s.replicaSetState.destroy(options);
959-
960-
// Destroy all connecting servers
961-
this.s.connectingServers.forEach(function(x) {
962-
x.destroy(options);
963-
});
964974

965975
// Clear out all monitoring
966976
for (var i = 0; i < this.intervalIds.length; i++) {
@@ -970,8 +980,18 @@ ReplSet.prototype.destroy = function(options) {
970980
// Reset list of intervalIds
971981
this.intervalIds = [];
972982

973-
// Emit toplogy closing event
974-
emitSDAMEvent(this, 'topologyClosed', { topologyId: this.id });
983+
if (destroyCount === 0) {
984+
serverDestroyed();
985+
return;
986+
}
987+
988+
// Destroy the replicaset
989+
this.s.replicaSetState.destroy(options, serverDestroyed);
990+
991+
// Destroy all connecting servers
992+
this.s.connectingServers.forEach(function(x) {
993+
x.destroy(options, serverDestroyed);
994+
});
975995
};
976996

977997
/**

lib/topologies/replset_state.js

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -116,31 +116,43 @@ ReplSetState.prototype.allServers = function(options) {
116116
return servers;
117117
};
118118

119-
ReplSetState.prototype.destroy = function(options) {
120-
// Destroy all sockets
121-
if (this.primary) this.primary.destroy(options);
122-
this.secondaries.forEach(function(x) {
123-
x.destroy(options);
124-
});
125-
this.arbiters.forEach(function(x) {
126-
x.destroy(options);
127-
});
128-
this.passives.forEach(function(x) {
129-
x.destroy(options);
130-
});
131-
this.ghosts.forEach(function(x) {
132-
x.destroy(options);
133-
});
134-
// Clear out the complete state
135-
this.secondaries = [];
136-
this.arbiters = [];
137-
this.passives = [];
138-
this.ghosts = [];
139-
this.unknownServers = [];
140-
this.set = {};
141-
this.primary = null;
142-
// Emit the topology changed
143-
emitTopologyDescriptionChanged(this);
119+
ReplSetState.prototype.destroy = function(options, callback) {
120+
const serversToDestroy = this.secondaries
121+
.concat(this.arbiters)
122+
.concat(this.passives)
123+
.concat(this.ghosts);
124+
if (this.primary) serversToDestroy.push(this.primary);
125+
126+
let serverCount = serversToDestroy.length;
127+
const serverDestroyed = () => {
128+
serverCount--;
129+
if (serverCount > 0) {
130+
return;
131+
}
132+
133+
// Clear out the complete state
134+
this.secondaries = [];
135+
this.arbiters = [];
136+
this.passives = [];
137+
this.ghosts = [];
138+
this.unknownServers = [];
139+
this.set = {};
140+
this.primary = null;
141+
142+
// Emit the topology changed
143+
emitTopologyDescriptionChanged(this);
144+
145+
if (typeof callback === 'function') {
146+
callback(null, null);
147+
}
148+
};
149+
150+
if (serverCount === 0) {
151+
serverDestroyed();
152+
return;
153+
}
154+
155+
serversToDestroy.forEach(server => server.destroy(options, serverDestroyed));
144156
};
145157

146158
ReplSetState.prototype.remove = function(server, options) {

0 commit comments

Comments
 (0)