X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fleast-used-worker-choice-strategy.ts;h=515b4e935db6e33fd5f3946c4f1b7cb0eb52a5f0;hb=f6bc9f26d8a0246bbee14b2b03d0bcc41b8aeb52;hp=86a1ef23f1871ca85c0376db73b3517666e977ba;hpb=1ab50fe5ad0b27a13a48047f9414d4138d43a5cd;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 86a1ef23..515b4e93 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.setRequiredStatistics(this.opts) + this.setTaskStatisticsRequirements(this.opts) } /** @inheritDoc */ @@ -41,28 +41,37 @@ export class LeastUsedWorkerChoiceStrategy< } /** @inheritDoc */ - public choose (): number { - const freeWorkerNodeKey = this.findFreeWorkerNodeKey() - if (freeWorkerNodeKey !== -1) { - return freeWorkerNodeKey - } - let minNumberOfTasks = Infinity - let leastUsedWorkerNodeKey!: number - for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) { - const tasksUsage = workerNode.tasksUsage - const workerTasks = tasksUsage.ran + tasksUsage.running - if (workerTasks === 0) { - return workerNodeKey - } else if (workerTasks < minNumberOfTasks) { - minNumberOfTasks = workerTasks - leastUsedWorkerNodeKey = workerNodeKey - } - } - return leastUsedWorkerNodeKey + public choose (): number | undefined { + const chosenWorkerNodeKey = this.leastUsedNextWorkerNodeKey() + this.assignChosenWorkerNodeKey(chosenWorkerNodeKey) + return this.nextWorkerNodeKey } /** @inheritDoc */ public remove (): boolean { return true } + + 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 + } }