Skip to content

Commit a6428d3

Browse files
authored
fix: acquire not resolving after destroying available resources (#44)
1 parent 0a3b3cf commit a6428d3

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/Pool.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,14 @@ export class Pool<RawResource> {
538538
await this._factory.destroy(resource);
539539
} finally {
540540
this._ensureMinimum();
541+
// Since we are removing the resource, we need to go ahead and
542+
// call _dispense() in order to ensure the resource is replaced (if appropriate)
543+
// and any pending resource requests are fulfilled.
544+
if (!this._draining) {
545+
process.nextTick(() => {
546+
this._dispense();
547+
});
548+
}
541549
}
542550
}
543551

test/integration/pool-test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,3 +598,29 @@ tap.test('pool does not leak expired resources to pending requests', (t) => {
598598
pool.release(clientA);
599599
});
600600
});
601+
602+
tap.test('acquire resolves after destroying the available resources', (t) => {
603+
const resourceFactory = new ResourceFactory();
604+
605+
const factory = {
606+
name: 'test22',
607+
create: resourceFactory.create.bind(resourceFactory),
608+
destroy: resourceFactory.destroy.bind(resourceFactory),
609+
validate: resourceFactory.validate.bind(resourceFactory),
610+
max: 1,
611+
min: 0,
612+
idleTimeoutMillis: 100,
613+
acquireTimeoutMillis: 100,
614+
};
615+
616+
const pool = new Pool(factory);
617+
618+
pool.acquire().then((resource1) => {
619+
pool.acquire().then((resource2) => {
620+
pool.release(resource2);
621+
622+
t.end();
623+
});
624+
pool.destroy(resource1);
625+
});
626+
});

0 commit comments

Comments
 (0)