X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fleast-used-worker-choice-strategy.ts;h=e72efda2eb7db69411cc324e2e003c64fa08f529;hb=4ba8492f17b13668853cfc15a1ec9ff8c8d7b179;hp=528bca1dc683adf0ac25cdd8f53ba4a2737b2294;hpb=1c6fe997dcb16b510da6587b992cd3f66d62a259;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 528bca1d..e72efda2 100644 --- a/src/pools/selection-strategies/least-used-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/least-used-worker-choice-strategy.ts @@ -11,8 +11,8 @@ import type { * 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, @@ -27,7 +27,7 @@ export class LeastUsedWorkerChoiceStrategy< opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS ) { super(pool, opts) - this.setTaskStatistics(this.opts) + this.setTaskStatisticsRequirements(this.opts) } /** @inheritDoc */ @@ -42,30 +42,33 @@ export class LeastUsedWorkerChoiceStrategy< /** @inheritDoc */ public choose (): number { - const freeWorkerNodeKey = this.findFreeWorkerNodeKey() - if (freeWorkerNodeKey !== -1) { - return freeWorkerNodeKey - } + return this.leastUsedNextWorkerNodeKey() + } + + /** @inheritDoc */ + public remove (): boolean { + return true + } + + private leastUsedNextWorkerNodeKey (): number { let minNumberOfTasks = Infinity - let leastUsedWorkerNodeKey!: number for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) { - const workerTaskStatistics = workerNode.workerUsage.tasks + const workerTaskStatistics = workerNode.usage.tasks const workerTasks = workerTaskStatistics.executed + workerTaskStatistics.executing + workerTaskStatistics.queued - if (workerTasks === 0) { - return workerNodeKey - } else if (workerTasks < minNumberOfTasks) { + if (this.isWorkerNodeEligible(workerNodeKey) && workerTasks === 0) { + this.nextWorkerNodeKey = workerNodeKey + break + } else if ( + this.isWorkerNodeEligible(workerNodeKey) && + workerTasks < minNumberOfTasks + ) { minNumberOfTasks = workerTasks - leastUsedWorkerNodeKey = workerNodeKey + this.nextWorkerNodeKey = workerNodeKey } } - return leastUsedWorkerNodeKey - } - - /** @inheritDoc */ - public remove (): boolean { - return true + return this.nextWorkerNodeKey } }