Skip to content

Commit 2d1ff40

Browse files
mbroadstdaprahamian
authored andcommitted
feat(pool): add support for resetting the connection pool
SDAM requires us to be able to rest the connection pool in certain conditions, but we have traditionally not done this in the node driver. This implements support for the reset, and unblocks us from implementing tests like "cursors survive primary stepdown" NODE-1682
1 parent 9ea7d95 commit 2d1ff40

File tree

1 file changed

+38
-17
lines changed

1 file changed

+38
-17
lines changed

lib/core/connection/pool.js

+38-17
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ var CONNECTED = 'connected';
2626
var DESTROYING = 'destroying';
2727
var DESTROYED = 'destroyed';
2828

29+
const CONNECTION_EVENTS = ['error', 'close', 'timeout', 'parseError', 'connect', 'message'];
30+
2931
var _id = 0;
3032

3133
/**
@@ -631,9 +633,6 @@ Pool.prototype.unref = function() {
631633
});
632634
};
633635

634-
// Events
635-
var events = ['error', 'close', 'timeout', 'parseError', 'connect', 'message'];
636-
637636
// Destroy the connections
638637
function destroy(self, connections, options, callback) {
639638
let connectionCount = connections.length;
@@ -669,10 +668,7 @@ function destroy(self, connections, options, callback) {
669668

670669
// Destroy all connections
671670
connections.forEach(conn => {
672-
for (var i = 0; i < events.length; i++) {
673-
conn.removeAllListeners(events[i]);
674-
}
675-
671+
CONNECTION_EVENTS.forEach(eventName => conn.removeAllListeners(eventName));
676672
conn.destroy(options, connectionDestroyed);
677673
});
678674
}
@@ -758,19 +754,44 @@ Pool.prototype.destroy = function(force, callback) {
758754
* @param {function} [callback]
759755
*/
760756
Pool.prototype.reset = function(callback) {
761-
// this.destroy(true, err => {
762-
// if (err && typeof callback === 'function') {
763-
// callback(err, null);
764-
// return;
765-
// }
757+
const connections = this.availableConnections.concat(this.inUseConnections);
758+
let connectionCount = connections.length;
759+
const connectionDestroyed = () => {
760+
connectionCount--;
761+
if (connectionCount > 0) {
762+
return;
763+
}
766764

767-
// stateTransition(this, DISCONNECTED);
768-
// this.connect();
765+
// clear all pool state
766+
this.inUseConnections = [];
767+
this.availableConnections = [];
768+
this.connectingConnections = 0;
769+
this.executing = false;
770+
this.reconnectConnection = null;
771+
this.numberOfConsecutiveTimeouts = 0;
772+
this.connectionIndex = 0;
773+
this.retriesLeft = this.options.reconnectTries;
774+
this.reconnectId = null;
775+
776+
// create an initial connection, and kick off execution again
777+
_createConnection(this);
769778

770-
// if (typeof callback === 'function') callback(null, null);
771-
// });
779+
if (typeof callback === 'function') {
780+
callback(null, null);
781+
}
782+
};
783+
784+
// if we already have no connections, just reset state and callback
785+
if (connectionCount === 0) {
786+
connectionDestroyed();
787+
return;
788+
}
772789

773-
if (typeof callback === 'function') callback();
790+
// destroy all connections
791+
connections.forEach(conn => {
792+
CONNECTION_EVENTS.forEach(eventName => conn.removeAllListeners(eventName));
793+
conn.destroy({ force: true }, connectionDestroyed);
794+
});
774795
};
775796

776797
// Prepare the buffer that Pool.prototype.write() uses to send to the server

0 commit comments

Comments
 (0)