1 import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
} from
'../../utils'
2 import type { IPool
} from
'../pool'
3 import type { IWorker
} from
'../worker'
4 import { AbstractWorkerChoiceStrategy
} from
'./abstract-worker-choice-strategy'
7 TaskStatisticsRequirements
,
8 WorkerChoiceStrategyOptions
9 } from
'./selection-strategies-types'
12 * Selects the worker with the least ELU.
14 * @typeParam Worker - Type of worker which manages the strategy.
15 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
16 * @typeParam Response - Type of execution response. This can only be serializable data.
18 export class LeastEluWorkerChoiceStrategy
<
19 Worker
extends IWorker
,
23 extends AbstractWorkerChoiceStrategy
<Worker
, Data
, Response
>
24 implements IWorkerChoiceStrategy
{
26 public readonly taskStatisticsRequirements
: TaskStatisticsRequirements
= {
46 pool
: IPool
<Worker
, Data
, Response
>,
47 opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
50 this.setTaskStatisticsRequirements(this.opts
)
54 public reset (): boolean {
59 public update (): boolean {
64 public choose (): number {
65 let minWorkerElu
= Infinity
66 for (const [workerNodeKey
, workerNode
] of this.pool
.workerNodes
.entries()) {
67 const workerUsage
= workerNode
.workerUsage
68 const workerElu
= workerUsage
.elu
?.active
.aggregate
?? 0
69 if (workerElu
=== 0) {
70 this.nextWorkerNodeId
= workerNodeKey
72 } else if (workerElu
< minWorkerElu
) {
73 minWorkerElu
= workerElu
74 this.nextWorkerNodeId
= workerNodeKey
77 return this.nextWorkerNodeId
81 public remove (): boolean {