refactor: align findFreeWorkerKey() return type with findIndex()
authorJérôme Benoit <jerome.benoit@sap.com>
Tue, 4 Apr 2023 00:20:42 +0000 (02:20 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Tue, 4 Apr 2023 00:20:42 +0000 (02:20 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/pools/abstract-pool.ts
src/pools/pool-internal.ts
src/pools/selection-strategies/dynamic-pool-worker-choice-strategy.ts
src/pools/selection-strategies/fair-share-worker-choice-strategy.ts
src/pools/selection-strategies/less-busy-worker-choice-strategy.ts
src/pools/selection-strategies/less-used-worker-choice-strategy.ts
src/pools/selection-strategies/round-robin-worker-choice-strategy.ts
src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts

index 14d9c24870b0c05083c13936a0997b93e9e5619e..66b1ea1da3f3ba47d93cda9c94d2b2a19cc63ac3 100644 (file)
@@ -177,16 +177,15 @@ export abstract class AbstractPool<
   protected internalGetBusyStatus (): boolean {
     return (
       this.numberOfRunningTasks >= this.numberOfWorkers &&
-      this.findFreeWorkerKey() === false
+      this.findFreeWorkerKey() === -1
     )
   }
 
   /** {@inheritDoc} */
-  public findFreeWorkerKey (): number | false {
-    const freeWorkerKey = this.workers.findIndex(workerItem => {
+  public findFreeWorkerKey (): number {
+    return this.workers.findIndex(workerItem => {
       return workerItem.tasksUsage.running === 0
     })
-    return freeWorkerKey !== -1 ? freeWorkerKey : false
   }
 
   /** {@inheritDoc} */
index 261a9d7beeb862258ebabceef5b69a71df091180..cf6c6a99b26235a3ee4a28042072d9d3d0d5f057 100644 (file)
@@ -73,7 +73,7 @@ export interface IPoolInternal<
    *
    * If no free worker is found, `false` is returned.
    *
-   * @returns A worker key if there is one, otherwise `false`.
+   * @returns A worker key if there is one, `-1` otherwise.
    */
-  findFreeWorkerKey: () => number | false
+  findFreeWorkerKey: () => number
 }
index 1658d176e961eda3295547f55e7050c5a8478143..54c95329ff67d7e948b23df7272b70b039dfa884 100644 (file)
@@ -16,10 +16,12 @@ import { getWorkerChoiceStrategy } from './selection-strategies-utils'
  * @typeParam Response - Type of response of execution. This can only be serializable data.
  */
 export class DynamicPoolWorkerChoiceStrategy<
-  Worker extends IPoolWorker,
-  Data,
-  Response
-> extends AbstractWorkerChoiceStrategy<Worker, Data, Response> {
+    Worker extends IPoolWorker,
+    Data,
+    Response
+  >
+  extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
+  implements IWorkerChoiceStrategy {
   private readonly workerChoiceStrategy: IWorkerChoiceStrategy
 
   /**
@@ -50,7 +52,7 @@ export class DynamicPoolWorkerChoiceStrategy<
   /** {@inheritDoc} */
   public choose (): number {
     const freeWorkerKey = this.pool.findFreeWorkerKey()
-    if (freeWorkerKey !== false) {
+    if (freeWorkerKey !== -1) {
       return freeWorkerKey
     }
 
index dbe10ce7181355fbf56a35c42551721f592efba2..57aafe3759544df2e33bdf67ff0c4185b12f7f7e 100644 (file)
@@ -1,6 +1,9 @@
 import type { IPoolWorker } from '../pool-worker'
 import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
-import type { RequiredStatistics } from './selection-strategies-types'
+import type {
+  IWorkerChoiceStrategy,
+  RequiredStatistics
+} from './selection-strategies-types'
 
 /**
  * Worker virtual task timestamp.
@@ -19,10 +22,12 @@ interface WorkerVirtualTaskTimestamp {
  * @typeParam Response - Type of response of execution. This can only be serializable data.
  */
 export class FairShareWorkerChoiceStrategy<
-  Worker extends IPoolWorker,
-  Data,
-  Response
-> extends AbstractWorkerChoiceStrategy<Worker, Data, Response> {
+    Worker extends IPoolWorker,
+    Data,
+    Response
+  >
+  extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
+  implements IWorkerChoiceStrategy {
   /** {@inheritDoc} */
   public readonly requiredStatistics: RequiredStatistics = {
     runTime: true
index 61e2d369db8e0b6280d03ca6d5277c5e63bef8c9..75109a2b2848af781704a0c41965cf7180ea27e7 100644 (file)
@@ -1,6 +1,9 @@
 import type { IPoolWorker } from '../pool-worker'
 import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
-import type { RequiredStatistics } from './selection-strategies-types'
+import type {
+  IWorkerChoiceStrategy,
+  RequiredStatistics
+} from './selection-strategies-types'
 
 /**
  * Selects the less busy worker.
@@ -10,10 +13,12 @@ import type { RequiredStatistics } from './selection-strategies-types'
  * @typeParam Response - Type of response of execution. This can only be serializable data.
  */
 export class LessBusyWorkerChoiceStrategy<
-  Worker extends IPoolWorker,
-  Data,
-  Response
-> extends AbstractWorkerChoiceStrategy<Worker, Data, Response> {
+    Worker extends IPoolWorker,
+    Data,
+    Response
+  >
+  extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
+  implements IWorkerChoiceStrategy {
   /** {@inheritDoc} */
   public readonly requiredStatistics: RequiredStatistics = {
     runTime: true
@@ -27,7 +32,7 @@ export class LessBusyWorkerChoiceStrategy<
   /** {@inheritDoc} */
   public choose (): number {
     const freeWorkerKey = this.pool.findFreeWorkerKey()
-    if (!this.isDynamicPool && freeWorkerKey !== false) {
+    if (!this.isDynamicPool && freeWorkerKey !== -1) {
       return freeWorkerKey
     }
     let minRunTime = Infinity
index 7fe56ef9dd2293ea33aaf3a9f4a2d3cda28be451..c83a012b313eaa80cee0999bb49c200cce0ad769 100644 (file)
@@ -1,5 +1,6 @@
 import type { IPoolWorker } from '../pool-worker'
 import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
+import type { IWorkerChoiceStrategy } from './selection-strategies-types'
 
 /**
  * Selects the less used worker.
@@ -9,10 +10,12 @@ import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
  * @typeParam Response - Type of response of execution. This can only be serializable data.
  */
 export class LessUsedWorkerChoiceStrategy<
-  Worker extends IPoolWorker,
-  Data,
-  Response
-> extends AbstractWorkerChoiceStrategy<Worker, Data, Response> {
+    Worker extends IPoolWorker,
+    Data,
+    Response
+  >
+  extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
+  implements IWorkerChoiceStrategy {
   /** {@inheritDoc} */
   public reset (): boolean {
     return true
@@ -21,7 +24,7 @@ export class LessUsedWorkerChoiceStrategy<
   /** {@inheritDoc} */
   public choose (): number {
     const freeWorkerKey = this.pool.findFreeWorkerKey()
-    if (!this.isDynamicPool && freeWorkerKey !== false) {
+    if (!this.isDynamicPool && freeWorkerKey !== -1) {
       return freeWorkerKey
     }
     let minNumberOfTasks = Infinity
index 3880ca391ac4ed980abda24a8740282e750a9b81..1499db945f8297181724db968f21b938d8f3618b 100644 (file)
@@ -1,5 +1,6 @@
 import type { IPoolWorker } from '../pool-worker'
 import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
+import type { IWorkerChoiceStrategy } from './selection-strategies-types'
 
 /**
  * Selects the next worker in a round robin fashion.
@@ -9,10 +10,12 @@ import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
  * @typeParam Response - Type of response of execution. This can only be serializable data.
  */
 export class RoundRobinWorkerChoiceStrategy<
-  Worker extends IPoolWorker,
-  Data,
-  Response
-> extends AbstractWorkerChoiceStrategy<Worker, Data, Response> {
+    Worker extends IPoolWorker,
+    Data,
+    Response
+  >
+  extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
+  implements IWorkerChoiceStrategy {
   /**
    * Id of the next worker.
    */
index 24a25b4d30de7fa553f500615b5b5e8e06efd972..ad42ee90602797171bcbb7d3cc33672817d64ec4 100644 (file)
@@ -2,7 +2,10 @@ import { cpus } from 'node:os'
 import type { IPoolInternal } from '../pool-internal'
 import type { IPoolWorker } from '../pool-worker'
 import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
-import type { RequiredStatistics } from './selection-strategies-types'
+import type {
+  IWorkerChoiceStrategy,
+  RequiredStatistics
+} from './selection-strategies-types'
 
 /**
  * Virtual task runtime.
@@ -21,10 +24,12 @@ interface TaskRunTime {
  * @typeParam Response - Type of response of execution. This can only be serializable data.
  */
 export class WeightedRoundRobinWorkerChoiceStrategy<
-  Worker extends IPoolWorker,
-  Data,
-  Response
-> extends AbstractWorkerChoiceStrategy<Worker, Data, Response> {
+    Worker extends IPoolWorker,
+    Data,
+    Response
+  >
+  extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
+  implements IWorkerChoiceStrategy {
   /** {@inheritDoc} */
   public readonly requiredStatistics: RequiredStatistics = {
     runTime: true