X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fleast-used-worker-choice-strategy.ts;h=53aa05eed641d557631e56e964b7d57739a4a7f8;hb=7afd4514165c3cbb3435e230654643c389990a3f;hp=86a1ef23f1871ca85c0376db73b3517666e977ba;hpb=adffe8df411f13f722c66cfe926de9c6f3c754a2;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..53aa05ee 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 */ @@ -42,23 +42,22 @@ 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 + const workerTaskStatistics = workerNode.usage.tasks + const workerTasks = + workerTaskStatistics.executed + + workerTaskStatistics.executing + + workerTaskStatistics.queued if (workerTasks === 0) { - return workerNodeKey + this.nextWorkerNodeId = workerNodeKey + break } else if (workerTasks < minNumberOfTasks) { minNumberOfTasks = workerTasks - leastUsedWorkerNodeKey = workerNodeKey + this.nextWorkerNodeId = workerNodeKey } } - return leastUsedWorkerNodeKey + return this.nextWorkerNodeId } /** @inheritDoc */