@@ -62,6 +62,7 @@ class Pool {
62
62
this . _pendingCreates = { }
63
63
this . _acquireRequests = { }
64
64
this . _activeResourceCounts = { }
65
+ this . _poolState = { }
65
66
this . _release = this . _release . bind ( this )
66
67
this . _log = log
67
68
this . _closed = false
@@ -189,10 +190,13 @@ class Pool {
189
190
190
191
const key = address . asKey ( )
191
192
let pool = this . _pools [ key ]
193
+ let poolState = this . _poolState [ key ]
192
194
if ( ! pool ) {
193
195
pool = [ ]
196
+ poolState = new PoolState ( )
194
197
this . _pools [ key ] = pool
195
198
this . _pendingCreates [ key ] = 0
199
+ this . _poolState [ key ] = poolState
196
200
}
197
201
while ( pool . length ) {
198
202
const resource = pool . pop ( )
@@ -231,7 +235,7 @@ class Pool {
231
235
let resource
232
236
try {
233
237
// Invoke callback that creates actual connection
234
- resource = await this . _create ( address , this . _release )
238
+ resource = await this . _create ( address , ( address , resource ) => this . _release ( poolState , address , resource ) )
235
239
236
240
resourceAcquired ( key , this . _activeResourceCounts )
237
241
if ( this . _log . isDebugEnabled ( ) ) {
@@ -243,11 +247,11 @@ class Pool {
243
247
return resource
244
248
}
245
249
246
- async _release ( address , resource ) {
250
+ async _release ( poolState , address , resource ) {
247
251
const key = address . asKey ( )
248
252
const pool = this . _pools [ key ]
249
253
250
- if ( pool ) {
254
+ if ( pool && poolState . isActive ( ) ) {
251
255
// there exist idle connections for the given key
252
256
if ( ! this . _validate ( resource ) ) {
253
257
if ( this . _log . isDebugEnabled ( ) ) {
@@ -295,19 +299,23 @@ class Pool {
295
299
296
300
async _purgeKey ( key ) {
297
301
const pool = this . _pools [ key ] || [ ]
302
+ const poolState = this . _poolState [ key ] || new PoolState ( )
298
303
while ( pool . length ) {
299
304
const resource = pool . pop ( )
300
305
if ( this . _removeIdleObserver ) {
301
306
this . _removeIdleObserver ( resource )
302
307
}
303
308
await this . _destroy ( resource )
304
309
}
310
+ poolState . close ( )
305
311
delete this . _pools [ key ]
312
+ delete this . _poolState [ key ]
306
313
}
307
314
308
315
_processPendingAcquireRequests ( address ) {
309
316
const key = address . asKey ( )
310
317
const requests = this . _acquireRequests [ key ]
318
+ const poolState = this . _poolState [ key ]
311
319
if ( requests ) {
312
320
const pendingRequest = requests . shift ( ) // pop a pending acquire request
313
321
@@ -326,7 +334,7 @@ class Pool {
326
334
if ( pendingRequest . isCompleted ( ) ) {
327
335
// request has been completed, most likely failed by a timeout
328
336
// return the acquired resource back to the pool
329
- this . _release ( address , resource )
337
+ this . _release ( poolState , address , resource )
330
338
} else {
331
339
// request is still pending and can be resolved with the newly acquired resource
332
340
pendingRequest . resolve ( resource ) // resolve the pending request with the acquired resource
@@ -404,4 +412,18 @@ class PendingRequest {
404
412
}
405
413
}
406
414
415
+ class PoolState {
416
+ constructor ( ) {
417
+ this . _active = true ;
418
+ }
419
+
420
+ isActive ( ) {
421
+ return this . _active ;
422
+ }
423
+
424
+ close ( ) {
425
+ this . _active = false ;
426
+ }
427
+ }
428
+
407
429
export default Pool
0 commit comments