fix: ensure the task concurrency is respected at queued task
[poolifier.git] / src / pools / abstract-pool.ts
index 0b359d93b6006c124c692fda70f9fb21926691f4..0d3d21832a715191e6efad9723c6a47454ae2215 100644 (file)
@@ -173,7 +173,15 @@ export abstract class AbstractPool<
 
   protected checkDynamicPoolSize (min: number, max: number): void {
     if (this.type === PoolTypes.dynamic) {
-      if (min > 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'
+        )
+      } else if (min > max) {
         throw new RangeError(
           'Cannot instantiate a dynamic pool with a maximum pool size inferior to the minimum pool size'
         )
@@ -1004,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