fix: only pickup a free worker with dynamic pool if the worker selection
[poolifier.git] / src / pools / selection-strategies / dynamic-pool-worker-choice-strategy.ts
index 54c95329ff67d7e948b23df7272b70b039dfa884..c0622613d8701c02360a04c7ab91abe10aff33d3 100644 (file)
@@ -51,17 +51,14 @@ export class DynamicPoolWorkerChoiceStrategy<
 
   /** {@inheritDoc} */
   public choose (): number {
-    const freeWorkerKey = this.pool.findFreeWorkerKey()
-    if (freeWorkerKey !== -1) {
-      return freeWorkerKey
-    }
-
     if (this.pool.busy) {
       return this.workerChoiceStrategy.choose()
     }
-
-    // All workers are busy, create a new worker
-    return this.createWorkerCallback()
+    const freeWorkerKey = this.pool.findFreeWorkerKey()
+    if (freeWorkerKey === -1) {
+      return this.createWorkerCallback()
+    }
+    return this.workerChoiceStrategy.choose()
   }
 
   /** {@inheritDoc} */