X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fleast-used-worker-choice-strategy.ts;h=d20c32174cc553c01b120fdd055217218fc4c314;hb=7790a494fdff6a2152b3153d02c9fbe8de11ed93;hp=e72efda2eb7db69411cc324e2e003c64fa08f529;hpb=8990357d855c45cd0063f24092bb58b4163ddb0a;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 e72efda2..d20c3217 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,10 @@ export class LeastUsedWorkerChoiceStrategy< } /** @inheritDoc */ - public choose (): number { - return this.leastUsedNextWorkerNodeKey() + public choose (): number | undefined { + this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey) + this.nextWorkerNodeKey = this.leastUsedNextWorkerNodeKey() + return this.nextWorkerNodeKey } /** @inheritDoc */ @@ -50,7 +52,8 @@ export class LeastUsedWorkerChoiceStrategy< return true } - private leastUsedNextWorkerNodeKey (): number { + private leastUsedNextWorkerNodeKey (): number | undefined { + let chosenWorkerNodeKey: number | undefined let minNumberOfTasks = Infinity for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) { const workerTaskStatistics = workerNode.usage.tasks @@ -58,17 +61,14 @@ export class LeastUsedWorkerChoiceStrategy< workerTaskStatistics.executed + workerTaskStatistics.executing + workerTaskStatistics.queued - if (this.isWorkerNodeEligible(workerNodeKey) && workerTasks === 0) { - this.nextWorkerNodeKey = workerNodeKey + if (workerTasks === 0) { + chosenWorkerNodeKey = workerNodeKey break - } else if ( - this.isWorkerNodeEligible(workerNodeKey) && - workerTasks < minNumberOfTasks - ) { + } else if (workerTasks < minNumberOfTasks) { minNumberOfTasks = workerTasks - this.nextWorkerNodeKey = workerNodeKey + chosenWorkerNodeKey = workerNodeKey } } - return this.nextWorkerNodeKey + return chosenWorkerNodeKey } }