X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fleast-used-worker-choice-strategy.ts;h=1ee6b7b8aeaf646488c20f7222e0a8fc45a27f7e;hb=88860dc62ddf62947b3b76703d4be7bf9137e401;hp=515b4e935db6e33fd5f3946c4f1b7cb0eb52a5f0;hpb=3882157ecadc80e686b319068bd73a9cc218e10c;p=poolifier.git diff --git a/src/pools/selection-strategies/least-used-worker-choice-strategy.ts b/src/pools/selection-strategies/least-used-worker-choice-strategy.ts index 515b4e93..1ee6b7b8 100644 --- a/src/pools/selection-strategies/least-used-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/least-used-worker-choice-strategy.ts @@ -1,15 +1,13 @@ -import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils' -import type { IPool } from '../pool' -import type { IWorker } from '../worker' -import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy' +import type { IPool } from '../pool.js' +import type { IWorker } from '../worker.js' +import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy.js' import type { IWorkerChoiceStrategy, - WorkerChoiceStrategyOptions -} from './selection-strategies-types' + WorkerChoiceStrategyOptions, +} from './selection-strategies-types.js' /** * Selects the least used worker. - * * @typeParam Worker - Type of worker which manages the strategy. * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data. * @typeParam Response - Type of execution response. This can only be structured-cloneable data. @@ -24,10 +22,9 @@ export class LeastUsedWorkerChoiceStrategy< /** @inheritDoc */ public constructor ( pool: IPool, - opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS + opts?: WorkerChoiceStrategyOptions ) { super(pool, opts) - this.setTaskStatisticsRequirements(this.opts) } /** @inheritDoc */ @@ -42,8 +39,8 @@ export class LeastUsedWorkerChoiceStrategy< /** @inheritDoc */ public choose (): number | undefined { - const chosenWorkerNodeKey = this.leastUsedNextWorkerNodeKey() - this.assignChosenWorkerNodeKey(chosenWorkerNodeKey) + this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey) + this.nextWorkerNodeKey = this.leastUsedNextWorkerNodeKey() return this.nextWorkerNodeKey } @@ -53,25 +50,16 @@ export class LeastUsedWorkerChoiceStrategy< } private leastUsedNextWorkerNodeKey (): number | undefined { - let minNumberOfTasks = Infinity - let chosenWorkerNodeKey: number | undefined - for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) { - const workerTaskStatistics = workerNode.usage.tasks - const workerTasks = - workerTaskStatistics.executed + - workerTaskStatistics.executing + - workerTaskStatistics.queued - if (this.isWorkerNodeEligible(workerNodeKey) && workerTasks === 0) { - chosenWorkerNodeKey = workerNodeKey - break - } else if ( - this.isWorkerNodeEligible(workerNodeKey) && - workerTasks < minNumberOfTasks - ) { - minNumberOfTasks = workerTasks - chosenWorkerNodeKey = workerNodeKey - } - } - return chosenWorkerNodeKey + return this.pool.workerNodes.reduce( + (minWorkerNodeKey, workerNode, workerNodeKey, workerNodes) => { + return this.isWorkerNodeReady(workerNodeKey) && + workerNode.usage.tasks.executing + workerNode.usage.tasks.queued < + workerNodes[minWorkerNodeKey].usage.tasks.executing + + workerNodes[minWorkerNodeKey].usage.tasks.queued + ? workerNodeKey + : minWorkerNodeKey + }, + 0 + ) } }