1 import type { IWorker
} from
'../worker'
2 import { AbstractWorkerChoiceStrategy
} from
'./abstract-worker-choice-strategy'
3 import type { IWorkerChoiceStrategy
} from
'./selection-strategies-types'
6 * Selects the less used worker.
8 * @typeParam Worker - Type of worker which manages the strategy.
9 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
10 * @typeParam Response - Type of response of execution. This can only be serializable data.
12 export class LessUsedWorkerChoiceStrategy
<
13 Worker
extends IWorker
,
17 extends AbstractWorkerChoiceStrategy
<Worker
, Data
, Response
>
18 implements IWorkerChoiceStrategy
{
20 public reset (): boolean {
25 public choose (): number {
26 const freeWorkerNodeKey
= this.pool
.findFreeWorkerNodeKey()
27 if (freeWorkerNodeKey
!== -1) {
28 return freeWorkerNodeKey
30 let minNumberOfTasks
= Infinity
31 let lessUsedWorkerNodeKey
!: number
32 for (const [index
, workerNode
] of this.pool
.workerNodes
.entries()) {
33 const tasksUsage
= workerNode
.tasksUsage
34 const workerTasks
= tasksUsage
.run
+ tasksUsage
.running
35 if (workerTasks
=== 0) {
37 } else if (workerTasks
< minNumberOfTasks
) {
38 minNumberOfTasks
= workerTasks
39 lessUsedWorkerNodeKey
= index
42 return lessUsedWorkerNodeKey
46 public remove (workerNodeKey
: number): boolean {