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=1416660efeb6aabf366295ebc79067ccc4df9897;hp=910d10a3ac320db8f3feac42eb43293181a806d1;hpb=9b1068374b1a52479b07e1e22b692289d5579237;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 910d10a3..515b4e93 100644 --- a/src/pools/selection-strategies/least-used-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/least-used-worker-choice-strategy.ts @@ -41,8 +41,9 @@ export class LeastUsedWorkerChoiceStrategy< } /** @inheritDoc */ - public choose (): number { - this.leastUsedNextWorkerNodeKey() + public choose (): number | undefined { + const chosenWorkerNodeKey = this.leastUsedNextWorkerNodeKey() + this.assignChosenWorkerNodeKey(chosenWorkerNodeKey) return this.nextWorkerNodeKey } @@ -51,24 +52,26 @@ export class LeastUsedWorkerChoiceStrategy< return true } - private leastUsedNextWorkerNodeKey (): void { + 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.isWorkerNodeReady(workerNodeKey) && workerTasks === 0) { - this.nextWorkerNodeKey = workerNodeKey + if (this.isWorkerNodeEligible(workerNodeKey) && workerTasks === 0) { + chosenWorkerNodeKey = workerNodeKey break } else if ( - this.isWorkerNodeReady(workerNodeKey) && + this.isWorkerNodeEligible(workerNodeKey) && workerTasks < minNumberOfTasks ) { minNumberOfTasks = workerTasks - this.nextWorkerNodeKey = workerNodeKey + chosenWorkerNodeKey = workerNodeKey } } + return chosenWorkerNodeKey } }