refactor: factor out stealing ratio conditions check into an helper
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 6 Sep 2024 10:39:38 +0000 (12:39 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 6 Sep 2024 10:39:38 +0000 (12:39 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/pools/abstract-pool.ts

index b883eb9646b71fb9b132063b672abb6fa4a789e6..91536f5e94d64ab16ef845d946dec4859cb44bf0 100644 (file)
@@ -240,13 +240,7 @@ export abstract class AbstractPool<
     if (
       this.cannotStealTask() ||
       this.backPressure ||
-      this.opts.tasksQueueOptions?.tasksStealingRatio === 0 ||
-      (this.info.stealingWorkerNodes ?? 0) >
-        Math.ceil(
-          this.workerNodes.length *
-            // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-            this.opts.tasksQueueOptions!.tasksStealingRatio!
-        )
+      this.isStealingRatioReached()
     ) {
       return
     }
@@ -299,14 +293,7 @@ export abstract class AbstractPool<
     }
     if (
       !workerNode.info.continuousStealing &&
-      (this.cannotStealTask() ||
-        this.opts.tasksQueueOptions?.tasksStealingRatio === 0 ||
-        (this.info.stealingWorkerNodes ?? 0) >
-          Math.ceil(
-            this.workerNodes.length *
-              // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-              this.opts.tasksQueueOptions!.tasksStealingRatio!
-          ))
+      (this.cannotStealTask() || this.isStealingRatioReached())
     ) {
       return
     }
@@ -341,6 +328,18 @@ export abstract class AbstractPool<
       })
   }
 
+  private readonly isStealingRatioReached = (): boolean => {
+    return (
+      this.opts.tasksQueueOptions?.tasksStealingRatio === 0 ||
+      (this.info.stealingWorkerNodes ?? 0) >
+        Math.ceil(
+          this.workerNodes.length *
+            // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+            this.opts.tasksQueueOptions!.tasksStealingRatio!
+        )
+    )
+  }
+
   /**
    * Whether the pool ready event has been emitted or not.
    */