Skip to content

Improve purge address of the Pool routine #994

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/bolt-connection/src/pool/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,16 +300,18 @@ class Pool {
async _purgeKey (key) {
const pool = this._pools[key] || []
const poolState = this._poolState[key] || new PoolState()
const destructionList = []
while (pool.length) {
const resource = pool.pop()
if (this._removeIdleObserver) {
this._removeIdleObserver(resource)
}
await this._destroy(resource)
destructionList.push(this._destroy(resource))
}
poolState.close()
delete this._pools[key]
delete this._poolState[key]
await Promise.all(destructionList)
}

_processPendingAcquireRequests (address) {
Expand Down
37 changes: 37 additions & 0 deletions packages/bolt-connection/test/pool/pool.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,43 @@ describe('#unit Pool', () => {
expect(resource1.observer).toBeFalsy()
expect(resource2.observer).toBeFalsy()
})

it('should purge resources in parallel', async () => {
const address = ServerAddress.fromUrl('bolt://localhost:7687')
let resourceCount = 0
const resourcesReleased = []
let resolveRelease
const releasePromise = new Promise((resolve) => {
resolveRelease = resolve
})

const pool = new Pool({
create: (server, release) =>
Promise.resolve(new Resource(server, resourceCount++, release)),
destroy: res => {
resourcesReleased.push(res)
resourceCount--
// Only destroy when the last resource
// get destroyed
if (resourceCount === 0) {
resolveRelease()
}
return releasePromise
},
validate: res => true,
})

const resource1 = await pool.acquire(address)
const resource2 = await pool.acquire(address)
await resource1.close()
await resource2.close()

await pool.purge(address)

expect(resourcesReleased).toEqual([
resource2, resource1
])
})
})

function expectNoPendingAcquisitionRequests (pool) {
Expand Down