@@ -19,6 +19,7 @@ const apm = require('./apm');
1919const Buffer = require ( 'safe-buffer' ) . Buffer ;
2020const connect = require ( './connect' ) ;
2121const updateSessionFromResponse = require ( '../sessions' ) . updateSessionFromResponse ;
22+ const eachAsync = require ( '../utils' ) . eachAsync ;
2223
2324var DISCONNECTED = 'disconnected' ;
2425var CONNECTING = 'connecting' ;
@@ -635,42 +636,35 @@ Pool.prototype.unref = function() {
635636
636637// Destroy the connections
637638function destroy ( self , connections , options , callback ) {
638- let connectionCount = connections . length ;
639- function connectionDestroyed ( ) {
640- connectionCount -- ;
641- if ( connectionCount > 0 ) {
642- return ;
643- }
644-
645- // clear all pool state
646- self . inUseConnections = [ ] ;
647- self . availableConnections = [ ] ;
648- self . connectingConnections = 0 ;
649- self . executing = false ;
650- self . queue = [ ] ;
651- self . reconnectConnection = null ;
652- self . numberOfConsecutiveTimeouts = 0 ;
653- self . connectionIndex = 0 ;
654- self . retriesLeft = self . options . reconnectTries ;
655- self . reconnectId = null ;
656-
657- // Set state to destroyed
658- stateTransition ( self , DESTROYED ) ;
659- if ( typeof callback === 'function' ) {
660- callback ( null , null ) ;
661- }
662- }
639+ eachAsync (
640+ connections ,
641+ ( conn , cb ) => {
642+ CONNECTION_EVENTS . forEach ( eventName => conn . removeAllListeners ( eventName ) ) ;
643+ conn . destroy ( options , cb ) ;
644+ } ,
645+ err => {
646+ if ( err ) {
647+ if ( typeof callback === 'function' ) callback ( err , null ) ;
648+ return ;
649+ }
663650
664- if ( connectionCount === 0 ) {
665- connectionDestroyed ( ) ;
666- return ;
667- }
651+ // clear all pool state
652+ self . inUseConnections = [ ] ;
653+ self . availableConnections = [ ] ;
654+ self . connectingConnections = 0 ;
655+ self . executing = false ;
656+ self . queue = [ ] ;
657+ self . reconnectConnection = null ;
658+ self . numberOfConsecutiveTimeouts = 0 ;
659+ self . connectionIndex = 0 ;
660+ self . retriesLeft = self . options . reconnectTries ;
661+ self . reconnectId = null ;
668662
669- // Destroy all connections
670- connections . forEach ( conn => {
671- CONNECTION_EVENTS . forEach ( eventName => conn . removeAllListeners ( eventName ) ) ;
672- conn . destroy ( options , connectionDestroyed ) ;
673- } ) ;
663+ // Set state to destroyed
664+ stateTransition ( self , DESTROYED ) ;
665+ if ( typeof callback === 'function' ) callback ( null , null ) ;
666+ }
667+ ) ;
674668}
675669
676670/**
@@ -755,43 +749,39 @@ Pool.prototype.destroy = function(force, callback) {
755749 */
756750Pool . prototype . reset = function ( callback ) {
757751 const connections = this . availableConnections . concat ( this . inUseConnections ) ;
758- let connectionCount = connections . length ;
759- const connectionDestroyed = ( ) => {
760- connectionCount -- ;
761- if ( connectionCount > 0 ) {
762- return ;
763- }
752+ eachAsync (
753+ connections ,
754+ ( conn , cb ) => {
755+ CONNECTION_EVENTS . forEach ( eventName => conn . removeAllListeners ( eventName ) ) ;
756+ conn . destroy ( { force : true } , cb ) ;
757+ } ,
758+ err => {
759+ if ( err ) {
760+ if ( typeof callback === 'function' ) {
761+ callback ( err , null ) ;
762+ return ;
763+ }
764+ }
764765
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 ) ;
778-
779- if ( typeof callback === 'function' ) {
780- callback ( null , null ) ;
766+ // clear all pool state
767+ this . inUseConnections = [ ] ;
768+ this . availableConnections = [ ] ;
769+ this . connectingConnections = 0 ;
770+ this . executing = false ;
771+ this . reconnectConnection = null ;
772+ this . numberOfConsecutiveTimeouts = 0 ;
773+ this . connectionIndex = 0 ;
774+ this . retriesLeft = this . options . reconnectTries ;
775+ this . reconnectId = null ;
776+
777+ // create an initial connection, and kick off execution again
778+ _createConnection ( this ) ;
779+
780+ if ( typeof callback === 'function' ) {
781+ callback ( null , null ) ;
782+ }
781783 }
782- } ;
783-
784- // if we already have no connections, just reset state and callback
785- if ( connectionCount === 0 ) {
786- connectionDestroyed ( ) ;
787- return ;
788- }
789-
790- // destroy all connections
791- connections . forEach ( conn => {
792- CONNECTION_EVENTS . forEach ( eventName => conn . removeAllListeners ( eventName ) ) ;
793- conn . destroy ( { force : true } , connectionDestroyed ) ;
794- } ) ;
784+ ) ;
795785} ;
796786
797787// Prepare the buffer that Pool.prototype.write() uses to send to the server
0 commit comments