From: Jérôme Benoit Date: Tue, 4 Apr 2023 00:20:42 +0000 (+0200) Subject: refactor: align findFreeWorkerKey() return type with findIndex() X-Git-Tag: v2.4.0-3~3 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=bf90656cacf88d2cfdd5b3262086ba55b2ff9818;p=poolifier.git refactor: align findFreeWorkerKey() return type with findIndex() Signed-off-by: Jérôme Benoit --- diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 14d9c248..66b1ea1d 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -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} */ diff --git a/src/pools/pool-internal.ts b/src/pools/pool-internal.ts index 261a9d7b..cf6c6a99 100644 --- a/src/pools/pool-internal.ts +++ b/src/pools/pool-internal.ts @@ -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 } diff --git a/src/pools/selection-strategies/dynamic-pool-worker-choice-strategy.ts b/src/pools/selection-strategies/dynamic-pool-worker-choice-strategy.ts index 1658d176..54c95329 100644 --- a/src/pools/selection-strategies/dynamic-pool-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/dynamic-pool-worker-choice-strategy.ts @@ -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 extends IPoolWorker, + Data, + Response + > + extends AbstractWorkerChoiceStrategy + 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 } diff --git a/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts b/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts index dbe10ce7..57aafe37 100644 --- a/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts @@ -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 extends IPoolWorker, + Data, + Response + > + extends AbstractWorkerChoiceStrategy + implements IWorkerChoiceStrategy { /** {@inheritDoc} */ public readonly requiredStatistics: RequiredStatistics = { runTime: true diff --git a/src/pools/selection-strategies/less-busy-worker-choice-strategy.ts b/src/pools/selection-strategies/less-busy-worker-choice-strategy.ts index 61e2d369..75109a2b 100644 --- a/src/pools/selection-strategies/less-busy-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/less-busy-worker-choice-strategy.ts @@ -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 extends IPoolWorker, + Data, + Response + > + extends AbstractWorkerChoiceStrategy + 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 diff --git a/src/pools/selection-strategies/less-used-worker-choice-strategy.ts b/src/pools/selection-strategies/less-used-worker-choice-strategy.ts index 7fe56ef9..c83a012b 100644 --- a/src/pools/selection-strategies/less-used-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/less-used-worker-choice-strategy.ts @@ -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 extends IPoolWorker, + Data, + Response + > + extends AbstractWorkerChoiceStrategy + 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 diff --git a/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts b/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts index 3880ca39..1499db94 100644 --- a/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts @@ -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 extends IPoolWorker, + Data, + Response + > + extends AbstractWorkerChoiceStrategy + implements IWorkerChoiceStrategy { /** * Id of the next worker. */ diff --git a/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts b/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts index 24a25b4d..ad42ee90 100644 --- a/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts @@ -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 extends IPoolWorker, + Data, + Response + > + extends AbstractWorkerChoiceStrategy + implements IWorkerChoiceStrategy { /** {@inheritDoc} */ public readonly requiredStatistics: RequiredStatistics = { runTime: true