X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fweighted-round-robin-worker-choice-strategy.ts;h=dc2e5df3f170a1c8d65118e705d6b0754cebf56b;hb=010d7020b316c0dd9a7a95a3ae1cba074ab2303e;hp=1bdc02fb189fcf5f21676af2593f2c24854c99dd;hpb=f06e48d8e14dcfe3277bd16b1bd2463136af13e6;p=poolifier.git diff --git a/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts b/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts index 1bdc02fb..dc2e5df3 100644 --- a/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts @@ -1,11 +1,13 @@ import { cpus } from 'node:os' -import type { IPoolInternal } from '../pool-internal' import type { IWorker } from '../worker' import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy' import type { IWorkerChoiceStrategy, - RequiredStatistics + RequiredStatistics, + WorkerChoiceStrategyOptions } from './selection-strategies-types' +import type { IPool } from '../pool' +import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils' /** * Virtual task runtime. @@ -21,7 +23,7 @@ interface TaskRunTime { * * @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 response of execution. This can only be serializable data. + * @typeParam Response - Type of execution response. This can only be serializable data. */ export class WeightedRoundRobinWorkerChoiceStrategy< Worker extends IWorker, @@ -53,13 +55,13 @@ export class WeightedRoundRobinWorkerChoiceStrategy< TaskRunTime >() - /** - * Constructs a worker choice strategy that selects with a weighted round robin scheduling algorithm. - * - * @param pool - The pool instance. - */ - public constructor (pool: IPoolInternal) { - super(pool) + /** @inheritDoc */ + public constructor ( + pool: IPool, + opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS + ) { + super(pool, opts) + this.checkOptions(opts) this.defaultWorkerWeight = this.computeWorkerWeight() this.initWorkersTaskRunTime() } @@ -146,7 +148,9 @@ export class WeightedRoundRobinWorkerChoiceStrategy< } private getWorkerVirtualTaskRunTime (workerNodeKey: number): number { - return this.pool.workerNodes[workerNodeKey].tasksUsage.avgRunTime + return this.requiredStatistics.medRunTime + ? this.pool.workerNodes[workerNodeKey].tasksUsage.medRunTime + : this.pool.workerNodes[workerNodeKey].tasksUsage.avgRunTime } private computeWorkerWeight (): number {