piscinajs / piscina

A fast, efficient Node.js Worker Thread Pool implementation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to check if queue has room before enqueuing

ronag opened this issue · comments

Currently the only way to know if further tasks can be scheduled is through runTask and catching an error, which is not optimal.

Is something like this possible to add?

if (!worker.isFull) {
  worker.runTask(task)
}

It would be actually really helpful, it should be straightforward.

The following set of lines does verify the queue depth size based on the configuration values and decides whether or not to enqueue/post tasks to any available worker.

piscina/src/index.ts

Lines 803 to 816 in bcae345

if (this.taskQueue.size > 0) {
const totalCapacity = this.options.maxQueue + this.pendingCapacity();
if (this.taskQueue.size >= totalCapacity) {
if (this.options.maxQueue === 0) {
return Promise.reject(Errors.NoTaskQueueAvailable());
} else {
return Promise.reject(Errors.TaskQueueAtLimit());
}
} else {
if (this.workers.size < this.options.maxThreads) {
this._addNewWorker();
}
this.taskQueue.push(taskInfo);
}

It is a matter of abstracting the maths from these two lines:

piscina/src/index.ts

Lines 804 to 805 in bcae345

const totalCapacity = this.options.maxQueue + this.pendingCapacity();
if (this.taskQueue.size >= totalCapacity) {

It can also be a refactoring opportunity 🙂

Duplicate of #126