+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 } from './selection-strategies-types'
+import type {
+ IWorkerChoiceStrategy,
+ WorkerChoiceStrategyOptions
+} from './selection-strategies-types'
/**
* Selects the less used worker.
>
extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
implements IWorkerChoiceStrategy {
+ /** @inheritDoc */
+ public constructor (
+ pool: IPool<Worker, Data, Response>,
+ opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
+ ) {
+ super(pool, opts)
+ this.setRequiredStatistics(this.opts)
+ }
+
/** @inheritDoc */
public reset (): boolean {
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 minNumberOfTasks = Infinity
let lessUsedWorkerNodeKey!: number
- for (const [index, workerNode] of this.pool.workerNodes.entries()) {
+ for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) {
const tasksUsage = workerNode.tasksUsage
const workerTasks = tasksUsage.run + tasksUsage.running
if (workerTasks === 0) {
- return index
+ return workerNodeKey
} else if (workerTasks < minNumberOfTasks) {
minNumberOfTasks = workerTasks
- lessUsedWorkerNodeKey = index
+ lessUsedWorkerNodeKey = workerNodeKey
}
}
return lessUsedWorkerNodeKey
}
/** @inheritDoc */
- public remove (workerNodeKey: number): boolean {
+ public remove (): boolean {
return true
}
}