@@ -62,7 +62,6 @@ class Pool {
62
62
this . _pendingCreates = { }
63
63
this . _acquireRequests = { }
64
64
this . _activeResourceCounts = { }
65
- this . _poolState = { }
66
65
this . _release = this . _release . bind ( this )
67
66
this . _log = log
68
67
this . _closed = false
@@ -184,13 +183,10 @@ class Pool {
184
183
185
184
const key = address . asKey ( )
186
185
let pool = this . _pools [ key ]
187
- let poolState = this . _poolState [ key ]
188
186
if ( ! pool ) {
189
- pool = [ ]
190
- poolState = new PoolState ( )
187
+ pool = new SingleAddressPool ( )
191
188
this . _pools [ key ] = pool
192
189
this . _pendingCreates [ key ] = 0
193
- this . _poolState [ key ] = poolState
194
190
}
195
191
while ( pool . length ) {
196
192
const resource = pool . pop ( )
@@ -229,7 +225,7 @@ class Pool {
229
225
let resource
230
226
try {
231
227
// Invoke callback that creates actual connection
232
- resource = await this . _create ( address , ( address , resource ) => this . _release ( poolState , address , resource ) )
228
+ resource = await this . _create ( address , ( address , resource ) => this . _release ( address , resource , pool ) )
233
229
234
230
resourceAcquired ( key , this . _activeResourceCounts )
235
231
if ( this . _log . isDebugEnabled ( ) ) {
@@ -241,11 +237,10 @@ class Pool {
241
237
return resource
242
238
}
243
239
244
- async _release ( poolState , address , resource ) {
240
+ async _release ( address , resource , pool ) {
245
241
const key = address . asKey ( )
246
- const pool = this . _pools [ key ]
247
242
248
- if ( pool && poolState . isActive ( ) ) {
243
+ if ( pool . isActive ( ) ) {
249
244
// there exist idle connections for the given key
250
245
if ( ! this . _validate ( resource ) ) {
251
246
if ( this . _log . isDebugEnabled ( ) ) {
@@ -288,28 +283,27 @@ class Pool {
288
283
}
289
284
resourceReleased ( key , this . _activeResourceCounts )
290
285
291
- this . _processPendingAcquireRequests ( address )
286
+ this . _processPendingAcquireRequests ( address , pool )
292
287
}
293
288
294
289
async _purgeKey ( key ) {
295
- const pool = this . _pools [ key ] || [ ]
296
- const poolState = this . _poolState [ key ] || new PoolState ( )
297
- while ( pool . length ) {
298
- const resource = pool . pop ( )
299
- if ( this . _removeIdleObserver ) {
300
- this . _removeIdleObserver ( resource )
290
+ const pool = this . _pools [ key ]
291
+ if ( pool ) {
292
+ while ( pool . length ) {
293
+ const resource = pool . pop ( )
294
+ if ( this . _removeIdleObserver ) {
295
+ this . _removeIdleObserver ( resource )
296
+ }
297
+ await this . _destroy ( resource )
301
298
}
302
- await this . _destroy ( resource )
299
+ pool . close ( )
300
+ delete this . _pools [ key ]
303
301
}
304
- poolState . close ( )
305
- delete this . _pools [ key ]
306
- delete this . _poolState [ key ]
307
302
}
308
303
309
- _processPendingAcquireRequests ( address ) {
304
+ _processPendingAcquireRequests ( address , pool ) {
310
305
const key = address . asKey ( )
311
306
const requests = this . _acquireRequests [ key ]
312
- const poolState = this . _poolState [ key ] || new PoolState ( )
313
307
if ( requests ) {
314
308
const pendingRequest = requests . shift ( ) // pop a pending acquire request
315
309
@@ -328,7 +322,7 @@ class Pool {
328
322
if ( pendingRequest . isCompleted ( ) ) {
329
323
// request has been completed, most likely failed by a timeout
330
324
// return the acquired resource back to the pool
331
- this . _release ( poolState , address , resource )
325
+ this . _release ( address , resource , pool )
332
326
} else {
333
327
// request is still pending and can be resolved with the newly acquired resource
334
328
pendingRequest . resolve ( resource ) // resolve the pending request with the acquired resource
@@ -415,9 +409,10 @@ class PendingRequest {
415
409
}
416
410
}
417
411
418
- class PoolState {
412
+ class SingleAddressPool {
419
413
constructor ( ) {
420
414
this . _active = true
415
+ this . _elements = [ ]
421
416
}
422
417
423
418
isActive ( ) {
@@ -427,6 +422,23 @@ class PoolState {
427
422
close ( ) {
428
423
this . _active = false
429
424
}
425
+
426
+ filter ( predicate ) {
427
+ this . _elements = this . _elements . filter ( predicate )
428
+ return this
429
+ }
430
+
431
+ get length ( ) {
432
+ return this . _elements . length
433
+ }
434
+
435
+ pop ( ) {
436
+ return this . _elements . pop ( )
437
+ }
438
+
439
+ push ( element ) {
440
+ return this . _elements . push ( element )
441
+ }
430
442
}
431
443
432
444
export default Pool
0 commit comments