1 import type { IPool
} from
'../pool.js'
2 import type { IWorker
} from
'../worker.js'
5 TaskStatisticsRequirements
,
6 WorkerChoiceStrategyOptions
,
7 } from
'./selection-strategies-types.js'
9 import { DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
} from
'../utils.js'
10 import { AbstractWorkerChoiceStrategy
} from
'./abstract-worker-choice-strategy.js'
13 * Selects the least busy worker.
14 * @typeParam Worker - Type of worker which manages the strategy.
15 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
16 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
18 export class LeastBusyWorkerChoiceStrategy
<
19 Worker
extends IWorker
,
23 extends AbstractWorkerChoiceStrategy
<Worker
, Data
, Response
>
24 implements IWorkerChoiceStrategy
{
26 public override
readonly taskStatisticsRequirements
: TaskStatisticsRequirements
=
28 elu
: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
,
43 pool
: IPool
<Worker
, Data
, Response
>,
44 opts
?: WorkerChoiceStrategyOptions
47 this.setTaskStatisticsRequirements(this.opts
)
50 private leastBusyNextWorkerNodeKey (): number | undefined {
51 return this.pool
.workerNodes
.reduce(
52 (minWorkerNodeKey
, workerNode
, workerNodeKey
, workerNodes
) => {
53 return this.isWorkerNodeReady(workerNodeKey
) &&
54 (workerNode
.usage
.waitTime
.aggregate
?? 0) +
55 (workerNode
.usage
.runTime
.aggregate
?? 0) <
56 (workerNodes
[minWorkerNodeKey
].usage
.waitTime
.aggregate
?? 0) +
57 (workerNodes
[minWorkerNodeKey
].usage
.runTime
.aggregate
?? 0)
66 public choose (): number | undefined {
67 this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey
)
68 this.nextWorkerNodeKey
= this.leastBusyNextWorkerNodeKey()
69 return this.nextWorkerNodeKey
73 public remove (): boolean {
78 public reset (): boolean {
83 public update (): boolean {