X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fleast-used-worker-choice-strategy.ts;h=4387d792ef951c2e602308b48d4caffd95e67d53;hb=172ed3de085bd6c59ffe877d48d4c49eeaffa8c7;hp=922659d33e6947e08e12df7b3bf0cd64dd55bf7a;hpb=5ea80606520ed06f815f4f7485ec9057ac23b176;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 922659d3..4387d792 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, @@ -37,31 +37,40 @@ export class LeastUsedWorkerChoiceStrategy< /** @inheritDoc */ public update (): boolean { + return true + } + + /** @inheritDoc */ + public choose (): number | undefined { + this.nextWorkerNodeKey = this.leastUsedNextWorkerNodeKey() + 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.workerUsage.tasks + const workerTaskStatistics = workerNode.usage.tasks const workerTasks = workerTaskStatistics.executed + workerTaskStatistics.executing + workerTaskStatistics.queued - if (workerTasks === 0) { - this.nextWorkerNodeId = workerNodeKey + if (this.isWorkerNodeEligible(workerNodeKey) && workerTasks === 0) { + chosenWorkerNodeKey = workerNodeKey break - } else if (workerTasks < minNumberOfTasks) { + } else if ( + this.isWorkerNodeEligible(workerNodeKey) && + workerTasks < minNumberOfTasks + ) { minNumberOfTasks = workerTasks - this.nextWorkerNodeId = workerNodeKey + chosenWorkerNodeKey = workerNodeKey } } - return true - } - - /** @inheritDoc */ - public choose (): number { - return this.nextWorkerNodeId - } - - /** @inheritDoc */ - public remove (): boolean { - return true + return chosenWorkerNodeKey } }