1 import { DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
} from
'../../utils'
2 import type { IPool
} from
'../pool'
3 import type { IWorker
} from
'../worker'
4 import { AbstractWorkerChoiceStrategy
} from
'./abstract-worker-choice-strategy'
7 InternalWorkerChoiceStrategyOptions
,
8 TaskStatisticsRequirements
9 } from
'./selection-strategies-types'
12 * 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 readonly taskStatisticsRequirements
: TaskStatisticsRequirements
= {
37 elu
: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
42 pool
: IPool
<Worker
, Data
, Response
>,
43 opts
: InternalWorkerChoiceStrategyOptions
46 this.setTaskStatisticsRequirements(this.opts
)
50 public reset (): boolean {
55 public update (): boolean {
60 public choose (): number | undefined {
61 this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey
)
62 this.nextWorkerNodeKey
= this.leastBusyNextWorkerNodeKey()
63 return this.nextWorkerNodeKey
67 public remove (): boolean {
71 private leastBusyNextWorkerNodeKey (): number | undefined {
72 return this.pool
.workerNodes
.reduce(
73 (minWorkerNodeKey
, workerNode
, workerNodeKey
, workerNodes
) => {
74 return this.isWorkerNodeReady(workerNodeKey
) &&
75 (workerNode
.usage
.runTime
.aggregate
?? 0) +
76 (workerNode
.usage
.waitTime
.aggregate
?? 0) <
77 (workerNodes
[minWorkerNodeKey
].usage
.runTime
.aggregate
?? 0) +
78 (workerNodes
[minWorkerNodeKey
].usage
.waitTime
.aggregate
?? 0)