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 WorkerChoiceStrategyOptions
8 } from
'./selection-strategies-types'
11 * Selects the least used 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 serializable data.
15 * @typeParam Response - Type of execution response. This can only be serializable data.
17 export class LeastUsedWorkerChoiceStrategy
<
18 Worker
extends IWorker
,
22 extends AbstractWorkerChoiceStrategy
<Worker
, Data
, Response
>
23 implements IWorkerChoiceStrategy
{
26 pool
: IPool
<Worker
, Data
, Response
>,
27 opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
30 this.setTaskStatisticsRequirements(this.opts
)
34 public reset (): boolean {
39 public update (): boolean {
40 let minNumberOfTasks
= Infinity
41 for (const [workerNodeKey
, workerNode
] of this.pool
.workerNodes
.entries()) {
42 const workerTaskStatistics
= workerNode
.workerUsage
.tasks
44 workerTaskStatistics
.executed
+
45 workerTaskStatistics
.executing
+
46 workerTaskStatistics
.queued
47 if (workerTasks
=== 0) {
48 this.nextWorkerNodeId
= workerNodeKey
50 } else if (workerTasks
< minNumberOfTasks
) {
51 minNumberOfTasks
= workerTasks
52 this.nextWorkerNodeId
= workerNodeKey
59 public choose (): number {
60 return this.nextWorkerNodeId
64 public remove (): boolean {