X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fleast-used-worker-choice-strategy.ts;h=6318affe0a306492fe7d1257d6a99e05e3938a67;hb=5b5321fd0b3ef10dd12b9aa21bf3ca9fa3970e76;hp=d20c32174cc553c01b120fdd055217218fc4c314;hpb=baca80f7921f47ead05d194fb782a3e310f4ea41;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 d20c3217..6318affe 100644 --- a/src/pools/selection-strategies/least-used-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/least-used-worker-choice-strategy.ts @@ -1,10 +1,9 @@ -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 { IWorkerChoiceStrategy, - WorkerChoiceStrategyOptions + InternalWorkerChoiceStrategyOptions } from './selection-strategies-types' /** @@ -24,10 +23,9 @@ export class LeastUsedWorkerChoiceStrategy< /** @inheritDoc */ public constructor ( pool: IPool, - opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS + opts: InternalWorkerChoiceStrategyOptions ) { super(pool, opts) - this.setTaskStatisticsRequirements(this.opts) } /** @inheritDoc */ @@ -53,22 +51,19 @@ export class LeastUsedWorkerChoiceStrategy< } private leastUsedNextWorkerNodeKey (): number | undefined { - let chosenWorkerNodeKey: number | undefined - let minNumberOfTasks = Infinity - for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) { - const workerTaskStatistics = workerNode.usage.tasks - const workerTasks = - workerTaskStatistics.executed + - workerTaskStatistics.executing + - workerTaskStatistics.queued - if (workerTasks === 0) { - chosenWorkerNodeKey = workerNodeKey - break - } else if (workerTasks < minNumberOfTasks) { - minNumberOfTasks = workerTasks - chosenWorkerNodeKey = workerNodeKey - } - } - return chosenWorkerNodeKey + return this.pool.workerNodes.reduce( + (minWorkerNodeKey, workerNode, workerNodeKey, workerNodes) => { + return this.isWorkerNodeReady(workerNodeKey) && + workerNode.usage.tasks.executed + + workerNode.usage.tasks.executing + + workerNode.usage.tasks.queued < + workerNodes[minWorkerNodeKey].usage.tasks.executed + + workerNodes[minWorkerNodeKey].usage.tasks.executing + + workerNodes[minWorkerNodeKey].usage.tasks.queued + ? workerNodeKey + : minWorkerNodeKey + }, + 0 + ) } }