X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fleast-busy-worker-choice-strategy.ts;h=338ae379d2663e3777366353691bdc5ab8639592;hb=9fe8fd698590c2494dc6793cfd8c08026fe88a31;hp=84476eedaf0c434f5bcce23aa7ca694f1bf9d267;hpb=db703c75cdbc0ec6dffbf701a4b4c4fd659cc8b2;p=poolifier.git diff --git a/src/pools/selection-strategies/least-busy-worker-choice-strategy.ts b/src/pools/selection-strategies/least-busy-worker-choice-strategy.ts index 84476eed..338ae379 100644 --- a/src/pools/selection-strategies/least-busy-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/least-busy-worker-choice-strategy.ts @@ -1,4 +1,7 @@ -import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils' +import { + DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS, + DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS +} from '../../utils' import type { IPool } from '../pool' import type { IWorker } from '../worker' import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy' @@ -12,8 +15,8 @@ import type { * Selects the least busy 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 LeastBusyWorkerChoiceStrategy< Worker extends IWorker, @@ -34,11 +37,7 @@ export class LeastBusyWorkerChoiceStrategy< average: false, median: false }, - elu: { - aggregate: false, - average: false, - median: false - } + elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS } /** @inheritDoc */ @@ -61,25 +60,34 @@ export class LeastBusyWorkerChoiceStrategy< } /** @inheritDoc */ - public choose (): number { + public choose (): number | undefined { + this.nextWorkerNodeKey = this.leastBusyNextWorkerNodeKey() + return this.nextWorkerNodeKey + } + + /** @inheritDoc */ + public remove (): boolean { + return true + } + + private leastBusyNextWorkerNodeKey (): number | undefined { let minTime = Infinity + let chosenWorkerNodeKey: number | undefined for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) { + if (!this.isWorkerNodeEligible(workerNodeKey)) { + continue + } const workerTime = - workerNode.workerUsage.runTime.aggregate + - workerNode.workerUsage.waitTime.aggregate + (workerNode.usage.runTime?.aggregate ?? 0) + + (workerNode.usage.waitTime?.aggregate ?? 0) if (workerTime === 0) { - this.nextWorkerNodeId = workerNodeKey + chosenWorkerNodeKey = workerNodeKey break } else if (workerTime < minTime) { minTime = workerTime - this.nextWorkerNodeId = workerNodeKey + chosenWorkerNodeKey = workerNodeKey } } - return this.nextWorkerNodeId - } - - /** @inheritDoc */ - public remove (): boolean { - return true + return chosenWorkerNodeKey } }