1 import type { IPool
} from
'../pool.js'
2 import { DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
} from
'../utils.js'
3 import type { IWorker
} from
'../worker.js'
4 import { AbstractWorkerChoiceStrategy
} from
'./abstract-worker-choice-strategy.js'
7 TaskStatisticsRequirements
,
8 WorkerChoiceStrategyOptions
,
9 } from
'./selection-strategies-types.js'
12 * Selects the least busy worker.
13 * @typeParam Worker - Type of worker which manages the strategy.
14 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
15 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
17 export class LeastBusyWorkerChoiceStrategy
<
18 Worker
extends IWorker
,
22 extends AbstractWorkerChoiceStrategy
<Worker
, Data
, Response
>
23 implements IWorkerChoiceStrategy
{
25 public readonly taskStatisticsRequirements
: TaskStatisticsRequirements
= {
36 elu
: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
,
41 pool
: IPool
<Worker
, Data
, Response
>,
42 opts
?: WorkerChoiceStrategyOptions
45 this.setTaskStatisticsRequirements(this.opts
)
49 public reset (): boolean {
54 public update (): boolean {
59 public choose (): number | undefined {
60 this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey
)
61 this.nextWorkerNodeKey
= this.leastBusyNextWorkerNodeKey()
62 return this.nextWorkerNodeKey
66 public remove (): boolean {
70 private leastBusyNextWorkerNodeKey (): number | undefined {
71 return this.pool
.workerNodes
.reduce(
72 (minWorkerNodeKey
, workerNode
, workerNodeKey
, workerNodes
) => {
73 return this.isWorkerNodeReady(workerNodeKey
) &&
74 (workerNode
.usage
.waitTime
.aggregate
?? 0) +
75 (workerNode
.usage
.runTime
.aggregate
?? 0) <
76 (workerNodes
[minWorkerNodeKey
].usage
.waitTime
.aggregate
?? 0) +
77 (workerNodes
[minWorkerNodeKey
].usage
.runTime
.aggregate
?? 0)