1 import type { IPool
} from
'../pool.js'
2 import type { IWorker
} from
'../worker.js'
3 import { AbstractWorkerChoiceStrategy
} from
'./abstract-worker-choice-strategy.js'
6 WorkerChoiceStrategyOptions
7 } from
'./selection-strategies-types.js'
10 * Selects the least used worker.
12 * @typeParam Worker - Type of worker which manages the strategy.
13 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
14 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
16 export class LeastUsedWorkerChoiceStrategy
<
17 Worker
extends IWorker
,
21 extends AbstractWorkerChoiceStrategy
<Worker
, Data
, Response
>
22 implements IWorkerChoiceStrategy
{
25 pool
: IPool
<Worker
, Data
, Response
>,
26 opts
?: WorkerChoiceStrategyOptions
32 public reset (): boolean {
37 public update (): boolean {
42 public choose (): number | undefined {
43 this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey
)
44 this.nextWorkerNodeKey
= this.leastUsedNextWorkerNodeKey()
45 return this.nextWorkerNodeKey
49 public remove (): boolean {
53 private leastUsedNextWorkerNodeKey (): number | undefined {
54 return this.pool
.workerNodes
.reduce(
55 (minWorkerNodeKey
, workerNode
, workerNodeKey
, workerNodes
) => {
56 return this.isWorkerNodeReady(workerNodeKey
) &&
57 workerNode
.usage
.tasks
.executing
+ workerNode
.usage
.tasks
.queued
<
58 workerNodes
[minWorkerNodeKey
].usage
.tasks
.executing
+
59 workerNodes
[minWorkerNodeKey
].usage
.tasks
.queued