Skip to content
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

fix: respect min pool configuration #23

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,15 @@ export class Pool<RawResource> {
this._pendingAcquires.push(deferred);
this._dispense();

let i, diff;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like copy paste from _ensureMinimum, just call that method here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a subtle difference, _ensureMinimum calls createResource without returning a promise. Using ensure minimum would hence cause the acquire method to return before the resource creation is done. This would have the potential of an unhandled exception if the exception occurs after the promise is returned.

Should ensure minimum be updated to return a promise?

if (this.size < this.minSize) {
diff = this.minSize - this.size;
for (i = 0; i < diff; i++) {
this._pendingAcquires.push(deferred);
this._dispense();
}
}

return deferred.promise();
}

Expand Down
25 changes: 25 additions & 0 deletions test/integration/pool-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,31 @@ tap.test('pool expands only to max limit', (t) => {
.catch(t.threw);
});

tap.test('pool expands to min', (t) => {
const resourceFactory = new ResourceFactory();

const factory = {
name: 'test1',
create: resourceFactory.create.bind(resourceFactory),
destroy: resourceFactory.destroy.bind(resourceFactory),
validate: resourceFactory.validate.bind(resourceFactory),
max: 3,
min: 2,
idleTimeoutMillis: 100,
acquireTimeoutMillis: 100,
};

const pool = new Pool(factory);

pool
.acquire()
.then((client) => {
t.equal(2, pool.using);
t.end();
})
.catch(t.threw);
});

tap.test('pool uses LIFO', (t) => {
const resourceFactory = new ResourceFactory();

Expand Down