@@ -26,6 +26,8 @@ var CONNECTED = 'connected';
26
26
var DESTROYING = 'destroying' ;
27
27
var DESTROYED = 'destroyed' ;
28
28
29
+ const CONNECTION_EVENTS = [ 'error' , 'close' , 'timeout' , 'parseError' , 'connect' , 'message' ] ;
30
+
29
31
var _id = 0 ;
30
32
31
33
/**
@@ -631,9 +633,6 @@ Pool.prototype.unref = function() {
631
633
} ) ;
632
634
} ;
633
635
634
- // Events
635
- var events = [ 'error' , 'close' , 'timeout' , 'parseError' , 'connect' , 'message' ] ;
636
-
637
636
// Destroy the connections
638
637
function destroy ( self , connections , options , callback ) {
639
638
let connectionCount = connections . length ;
@@ -669,10 +668,7 @@ function destroy(self, connections, options, callback) {
669
668
670
669
// Destroy all connections
671
670
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 ) ) ;
676
672
conn . destroy ( options , connectionDestroyed ) ;
677
673
} ) ;
678
674
}
@@ -758,19 +754,44 @@ Pool.prototype.destroy = function(force, callback) {
758
754
* @param {function } [callback]
759
755
*/
760
756
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
+ }
766
764
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 ) ;
769
778
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
+ }
772
789
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
+ } ) ;
774
795
} ;
775
796
776
797
// Prepare the buffer that Pool.prototype.write() uses to send to the server
0 commit comments