+import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils'
+import type { IPool } from '../pool'
import type { IWorker } from '../worker'
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 LessBusyWorkerChoiceStrategy<
Worker extends IWorker,
public readonly requiredStatistics: RequiredStatistics = {
runTime: true,
avgRunTime: false,
- medRunTime: false
+ medRunTime: false,
+ waitTime: false,
+ avgWaitTime: false,
+ medWaitTime: false
+ }
+
+ /** @inheritDoc */
+ public constructor (
+ pool: IPool<Worker, Data, Response>,
+ opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
+ ) {
+ super(pool, opts)
+ this.setRequiredStatistics(this.opts)
}
/** @inheritDoc */
return true
}
+ /** @inheritDoc */
+ public update (): boolean {
+ return true
+ }
+
/** @inheritDoc */
public choose (): number {
- const freeWorkerNodeKey = this.pool.findFreeWorkerNodeKey()
+ const freeWorkerNodeKey = this.findFreeWorkerNodeKey()
if (freeWorkerNodeKey !== -1) {
return freeWorkerNodeKey
}
let minRunTime = Infinity
let lessBusyWorkerNodeKey!: number
- for (const [index, workerNode] of this.pool.workerNodes.entries()) {
+ for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) {
const workerRunTime = workerNode.tasksUsage.runTime
if (workerRunTime === 0) {
- return index
+ return workerNodeKey
} else if (workerRunTime < minRunTime) {
minRunTime = workerRunTime
- lessBusyWorkerNodeKey = index
+ lessBusyWorkerNodeKey = workerNodeKey
}
}
return lessBusyWorkerNodeKey
}
/** @inheritDoc */
- public remove (workerNodeKey: number): boolean {
+ public remove (): boolean {
return true
}
}