-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'
* 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,
average: false,
median: false
},
- elu: false
+ elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
}
/** @inheritDoc */
/** @inheritDoc */
public choose (): number {
+ return this.leastBusyNextWorkerNodeKey()
+ }
+
+ /** @inheritDoc */
+ public remove (): boolean {
+ return true
+ }
+
+ private leastBusyNextWorkerNodeKey (): number {
let minTime = Infinity
- let leastBusyWorkerNodeKey!: number
for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) {
const workerTime =
- workerNode.workerUsage.runTime.aggregate +
- workerNode.workerUsage.waitTime.aggregate
- if (workerTime === 0) {
- return workerNodeKey
- } else if (workerTime < minTime) {
+ (workerNode.usage.runTime?.aggregate ?? 0) +
+ (workerNode.usage.waitTime?.aggregate ?? 0)
+ if (this.isWorkerNodeReady(workerNodeKey) && workerTime === 0) {
+ this.nextWorkerNodeKey = workerNodeKey
+ break
+ } else if (
+ this.isWorkerNodeReady(workerNodeKey) &&
+ workerTime < minTime
+ ) {
minTime = workerTime
- leastBusyWorkerNodeKey = workerNodeKey
+ this.nextWorkerNodeKey = workerNodeKey
}
}
- return leastBusyWorkerNodeKey
- }
-
- /** @inheritDoc */
- public remove (): boolean {
- return true
+ return this.nextWorkerNodeKey
}
}