2 DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
,
3 DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
5 import type { IPool
} from
'../pool'
6 import type { IWorker
} from
'../worker'
7 import { AbstractWorkerChoiceStrategy
} from
'./abstract-worker-choice-strategy'
10 TaskStatisticsRequirements
,
11 WorkerChoiceStrategyOptions
12 } from
'./selection-strategies-types'
15 * Selects the least busy worker.
17 * @typeParam Worker - Type of worker which manages the strategy.
18 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
19 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
21 export class LeastBusyWorkerChoiceStrategy
<
22 Worker
extends IWorker
,
26 extends AbstractWorkerChoiceStrategy
<Worker
, Data
, Response
>
27 implements IWorkerChoiceStrategy
{
29 public readonly taskStatisticsRequirements
: TaskStatisticsRequirements
= {
40 elu
: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
45 pool
: IPool
<Worker
, Data
, Response
>,
46 opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
49 this.setTaskStatisticsRequirements(this.opts
)
53 public reset (): boolean {
58 public update (): boolean {
63 public choose (): number | undefined {
64 this.nextWorkerNodeKey
= this.leastBusyNextWorkerNodeKey()
65 return this.nextWorkerNodeKey
69 public remove (): boolean {
73 private leastBusyNextWorkerNodeKey (): number | undefined {
74 let minTime
= Infinity
75 let chosenWorkerNodeKey
: number | undefined
76 for (const [workerNodeKey
, workerNode
] of this.pool
.workerNodes
.entries()) {
78 (workerNode
.usage
.runTime
?.aggregate
?? 0) +
79 (workerNode
.usage
.waitTime
?.aggregate
?? 0)
80 if (this.isWorkerNodeEligible(workerNodeKey
) && workerTime
=== 0) {
81 chosenWorkerNodeKey
= workerNodeKey
84 this.isWorkerNodeEligible(workerNodeKey
) &&
88 chosenWorkerNodeKey
= workerNodeKey
91 return chosenWorkerNodeKey