fix: ensure the task concurrency is respected at queued task
[poolifier.git] / src / pools / abstract-pool.ts
index 1222d686b847df02f2f9f90adced68d3cebb6b8f..0d3d21832a715191e6efad9723c6a47454ae2215 100644 (file)
@@ -173,7 +173,11 @@ export abstract class AbstractPool<
 
   protected checkDynamicPoolSize (min: number, max: number): void {
     if (this.type === PoolTypes.dynamic) {
-      if (!Number.isSafeInteger(max)) {
+      if (max == null) {
+        throw new Error(
+          'Cannot instantiate a dynamic pool without specifying the maximum pool size'
+        )
+      } else if (!Number.isSafeInteger(max)) {
         throw new TypeError(
           'Cannot instantiate a dynamic pool with a non safe integer maximum pool size'
         )
@@ -1008,7 +1012,10 @@ export abstract class AbstractPool<
           workerInfo.ready &&
           workerNode.usage.tasks.queued === 0
         ) {
-          if (workerNode.usage.tasks.executing === 0) {
+          if (
+            this.workerNodes[workerNodeId].usage.tasks.executing <
+            (this.opts.tasksQueueOptions?.concurrency as number)
+          ) {
             executeTask = true
           }
           targetWorkerNodeKey = workerNodeId