Skip to content

Commit 131aa73

Browse files
committed
Refactory Pool module
The state of the pool and the resource list weren't bounded togheter as it supposed to. This scenario makes the code prune to bugs and incosistency in the long run. Merge the resouse list (a.k.a. Pool) and PoolState in the same object makes the code easier to understand and more coesive.
1 parent bc6cc8f commit 131aa73

File tree

1 file changed

+36
-24
lines changed
  • packages/bolt-connection/src/pool

1 file changed

+36
-24
lines changed

packages/bolt-connection/src/pool/pool.js

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ class Pool {
6262
this._pendingCreates = {}
6363
this._acquireRequests = {}
6464
this._activeResourceCounts = {}
65-
this._poolState = {}
6665
this._release = this._release.bind(this)
6766
this._log = log
6867
this._closed = false
@@ -184,13 +183,10 @@ class Pool {
184183

185184
const key = address.asKey()
186185
let pool = this._pools[key]
187-
let poolState = this._poolState[key]
188186
if (!pool) {
189-
pool = []
190-
poolState = new PoolState()
187+
pool = new SingleAddressPool()
191188
this._pools[key] = pool
192189
this._pendingCreates[key] = 0
193-
this._poolState[key] = poolState
194190
}
195191
while (pool.length) {
196192
const resource = pool.pop()
@@ -229,7 +225,7 @@ class Pool {
229225
let resource
230226
try {
231227
// 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))
233229

234230
resourceAcquired(key, this._activeResourceCounts)
235231
if (this._log.isDebugEnabled()) {
@@ -241,11 +237,10 @@ class Pool {
241237
return resource
242238
}
243239

244-
async _release (poolState, address, resource) {
240+
async _release (address, resource, pool) {
245241
const key = address.asKey()
246-
const pool = this._pools[key]
247242

248-
if (pool && poolState.isActive()) {
243+
if (pool.isActive()) {
249244
// there exist idle connections for the given key
250245
if (!this._validate(resource)) {
251246
if (this._log.isDebugEnabled()) {
@@ -288,28 +283,27 @@ class Pool {
288283
}
289284
resourceReleased(key, this._activeResourceCounts)
290285

291-
this._processPendingAcquireRequests(address)
286+
this._processPendingAcquireRequests(address, pool)
292287
}
293288

294289
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)
301298
}
302-
await this._destroy(resource)
299+
pool.close()
300+
delete this._pools[key]
303301
}
304-
poolState.close()
305-
delete this._pools[key]
306-
delete this._poolState[key]
307302
}
308303

309-
_processPendingAcquireRequests (address) {
304+
_processPendingAcquireRequests (address, pool) {
310305
const key = address.asKey()
311306
const requests = this._acquireRequests[key]
312-
const poolState = this._poolState[key] || new PoolState()
313307
if (requests) {
314308
const pendingRequest = requests.shift() // pop a pending acquire request
315309

@@ -328,7 +322,7 @@ class Pool {
328322
if (pendingRequest.isCompleted()) {
329323
// request has been completed, most likely failed by a timeout
330324
// return the acquired resource back to the pool
331-
this._release(poolState, address, resource)
325+
this._release(address, resource, pool)
332326
} else {
333327
// request is still pending and can be resolved with the newly acquired resource
334328
pendingRequest.resolve(resource) // resolve the pending request with the acquired resource
@@ -415,9 +409,10 @@ class PendingRequest {
415409
}
416410
}
417411

418-
class PoolState {
412+
class SingleAddressPool {
419413
constructor () {
420414
this._active = true
415+
this._elements = []
421416
}
422417

423418
isActive () {
@@ -427,6 +422,23 @@ class PoolState {
427422
close () {
428423
this._active = false
429424
}
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+
}
430442
}
431443

432444
export default Pool

0 commit comments

Comments
 (0)