Skip to content
Open
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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ try {
}
}

// returns the maximum number of resources allowed by the pool. This can be configured when the pool is created.
pool.numMax();

// returns the minimum number of resources allowed by the pool. This can be configured when the pool is created.
pool.numMin();

// releases the resource.
pool.release(resource);

Expand All @@ -115,6 +121,9 @@ pool.numPendingAcquires();
// how many asynchronous create calls are running
pool.numPendingCreates();

// the number of additional resources which can be created before the maximum is reached. This already deducts the number of resources that are currently being created.
pool.numRemainingCapacity();

// waits for all resources to be returned to the pool and destroys them.
// pool cannot be used after this.
await pool.destroy();
Expand Down
17 changes: 17 additions & 0 deletions src/Pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,14 @@ export class Pool<T> {
this.eventId = 1;
}

numMax() {
return this.max;
}

numMin() {
return this.min;
}

numUsed() {
return this.used.length;
}
Expand All @@ -184,6 +192,15 @@ export class Pool<T> {
return this.pendingCreates.length;
}

numRemainingCapacity() {
const demand =
this.numUsed() +
this.numPendingCreates() +
this.numPendingValidations() +
this.numPendingAcquires();
return Math.max(0, this.numMax() - demand);
}

acquire() {
const eventId = this.eventId++;
this._executeEventHandlers('acquireRequest', eventId);
Expand Down
Loading