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=3c9123c7bc41a8815d7371d591ae88e38637bc42;hp=aaa6c21dc0f3dc7654a4b4d7986d44f809ef96b0;hpb=db703c75cdbc0ec6dffbf701a4b4c4fd659cc8b2;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 aaa6c21d..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,18 +1,17 @@ -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' /** * 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 serializable data. - * @typeParam Response - Type of execution response. This can only be serializable data. + * @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. */ export class LeastUsedWorkerChoiceStrategy< Worker extends IWorker, @@ -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 */ @@ -41,27 +39,31 @@ export class LeastUsedWorkerChoiceStrategy< } /** @inheritDoc */ - public choose (): number { - let minNumberOfTasks = Infinity - for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) { - const workerTaskStatistics = workerNode.workerUsage.tasks - const workerTasks = - workerTaskStatistics.executed + - workerTaskStatistics.executing + - workerTaskStatistics.queued - if (workerTasks === 0) { - this.nextWorkerNodeId = workerNodeKey - break - } else if (workerTasks < minNumberOfTasks) { - minNumberOfTasks = workerTasks - this.nextWorkerNodeId = workerNodeKey - } - } - return this.nextWorkerNodeId + public choose (): number | undefined { + this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey) + this.nextWorkerNodeKey = this.leastUsedNextWorkerNodeKey() + return this.nextWorkerNodeKey } /** @inheritDoc */ public remove (): boolean { return true } + + private leastUsedNextWorkerNodeKey (): number | undefined { + 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 + ) + } }