X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fleast-used-worker-choice-strategy.ts;h=1bd8d059fd5f9bc40484db9bbece55d319eddcbc;hb=36ed976c290ce30ad1e027f6f93e76652e530737;hp=d0adbcd7b77c4ef095f25fced318148498f42935;hpb=9fe8fd698590c2494dc6793cfd8c08026fe88a31;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 d0adbcd7..1bd8d059 100644 --- a/src/pools/selection-strategies/least-used-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/least-used-worker-choice-strategy.ts @@ -42,6 +42,7 @@ export class LeastUsedWorkerChoiceStrategy< /** @inheritDoc */ public choose (): number | undefined { + this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey) this.nextWorkerNodeKey = this.leastUsedNextWorkerNodeKey() return this.nextWorkerNodeKey } @@ -52,25 +53,19 @@ export class LeastUsedWorkerChoiceStrategy< } private leastUsedNextWorkerNodeKey (): number | undefined { - let minNumberOfTasks = Infinity - let chosenWorkerNodeKey: number | undefined - for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) { - if (!this.isWorkerNodeEligible(workerNodeKey)) { - continue - } - const workerTaskStatistics = workerNode.usage.tasks - const workerTasks = - workerTaskStatistics.executed + - workerTaskStatistics.executing + - workerTaskStatistics.queued - if (workerTasks === 0) { - chosenWorkerNodeKey = workerNodeKey - break - } else if (workerTasks < minNumberOfTasks) { - minNumberOfTasks = workerTasks - chosenWorkerNodeKey = workerNodeKey - } - } - return chosenWorkerNodeKey + return this.pool.workerNodes.reduce( + (minWorkerNodeKey, workerNode, workerNodeKey, workerNodes) => { + return this.isWorkerNodeReady(workerNodeKey) && + workerNode.usage.tasks.executed + + workerNode.usage.tasks.executing + + workerNode.usage.tasks.queued < + workerNodes[minWorkerNodeKey].usage.tasks.executed + + workerNodes[minWorkerNodeKey].usage.tasks.executing + + workerNodes[minWorkerNodeKey].usage.tasks.queued + ? workerNodeKey + : minWorkerNodeKey + }, + 0 + ) } }