X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fleast-used-worker-choice-strategy.ts;h=1f7033214d6a4735c3064df290acc2ce48cb3e72;hb=7c7bb28936c5de8829dba0794760498077d3ea90;hp=5c266f282d229fd5c5647e3305237ac1ee33c865;hpb=e102732c0e3966b81834b2c0bdd087eb051162ad;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 5c266f28..1f703321 100644 --- a/src/pools/selection-strategies/least-used-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/least-used-worker-choice-strategy.ts @@ -4,6 +4,7 @@ import type { IWorker } from '../worker' import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy' import type { IWorkerChoiceStrategy, + StrategyPolicy, WorkerChoiceStrategyOptions } from './selection-strategies-types' @@ -21,6 +22,12 @@ export class LeastUsedWorkerChoiceStrategy< > extends AbstractWorkerChoiceStrategy implements IWorkerChoiceStrategy { + /** @inheritDoc */ + public readonly strategyPolicy: StrategyPolicy = { + dynamicWorkerUsage: false, + dynamicWorkerReady: true + } + /** @inheritDoc */ public constructor ( pool: IPool, @@ -41,27 +48,37 @@ export class LeastUsedWorkerChoiceStrategy< } /** @inheritDoc */ - public choose (): number { + public choose (): number | undefined { + const chosenWorkerNodeKey = this.leastUsedNextWorkerNodeKey() + this.assignChosenWorkerNodeKey(chosenWorkerNodeKey) + 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 this.nextWorkerNodeId - } - - /** @inheritDoc */ - public remove (): boolean { - return true + return chosenWorkerNodeKey } }