@@ -156,16 +156,21 @@ class Pool extends EventEmitter {
156
156
throw new Error ( 'unexpected condition' )
157
157
}
158
158
159
- _remove ( client ) {
159
+ _remove ( client , callback ) {
160
160
const removed = removeWhere ( this . _idle , ( item ) => item . client === client )
161
161
162
162
if ( removed !== undefined ) {
163
163
clearTimeout ( removed . timeoutId )
164
164
}
165
165
166
166
this . _clients = this . _clients . filter ( ( c ) => c !== client )
167
- client . end ( )
168
- this . emit ( 'remove' , client )
167
+ client . end ( ( ) => {
168
+ this . emit ( 'remove' , client )
169
+
170
+ if ( typeof callback === "function" ) {
171
+ callback ( )
172
+ }
173
+ } )
169
174
}
170
175
171
176
connect ( cb ) {
@@ -342,17 +347,15 @@ class Pool extends EventEmitter {
342
347
if ( client . _poolUseCount >= this . options . maxUses ) {
343
348
this . log ( 'remove expended client' )
344
349
}
345
- this . _remove ( client )
346
- this . _pulseQueue ( )
347
- return
350
+
351
+ return this . _remove ( client , this . _pulseQueue . bind ( this ) )
348
352
}
349
353
350
354
const isExpired = this . _expired . has ( client )
351
355
if ( isExpired ) {
352
356
this . log ( 'remove expired client' )
353
357
this . _expired . delete ( client )
354
- this . _remove ( client )
355
- this . _pulseQueue ( )
358
+ this . _remove ( client , this . _pulseQueue . bind ( this ) )
356
359
return
357
360
}
358
361
@@ -361,7 +364,7 @@ class Pool extends EventEmitter {
361
364
if ( this . options . idleTimeoutMillis ) {
362
365
tid = setTimeout ( ( ) => {
363
366
this . log ( 'remove idle client' )
364
- this . _remove ( client )
367
+ this . _remove ( client , this . _pulseQueue . bind ( this ) )
365
368
} , this . options . idleTimeoutMillis )
366
369
367
370
if ( this . options . allowExitOnIdle ) {
0 commit comments