import { cpus } from 'node:os'
-import type { IPoolInternal } from '../pool-internal'
import type { IWorker } from '../worker'
+import type { IPool } from '../pool'
+import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils'
import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
import type {
IWorkerChoiceStrategy,
- RequiredStatistics
+ RequiredStatistics,
+ WorkerChoiceStrategyOptions
} from './selection-strategies-types'
/**
*
* @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,
TaskRunTime
>()
- /**
- * Constructs a worker choice strategy that selects with a weighted round robin scheduling algorithm.
- *
- * @param pool - The pool instance.
- */
- public constructor (pool: IPoolInternal<Worker, Data, Response>) {
- super(pool)
+ /** @inheritDoc */
+ public constructor (
+ pool: IPool<Worker, Data, Response>,
+ opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
+ ) {
+ super(pool, opts)
+ this.checkOptions(this.opts)
this.defaultWorkerWeight = this.computeWorkerWeight()
this.initWorkersTaskRunTime()
}
}
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 {