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'
8 WorkerChoiceStrategyOptions
9 } from
'./selection-strategies-types'
12 * Selects the less 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 serializable data.
16 * @typeParam Response - Type of execution response. This can only be serializable data.
18 export class LessBusyWorkerChoiceStrategy
<
19 Worker
extends IWorker
,
23 extends AbstractWorkerChoiceStrategy
<Worker
, Data
, Response
>
24 implements IWorkerChoiceStrategy
{
26 public readonly requiredStatistics
: RequiredStatistics
= {
34 pool
: IPool
<Worker
, Data
, Response
>,
35 opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
38 this.checkOptions(this.opts
)
42 public reset (): boolean {
47 public choose (): number {
48 const freeWorkerNodeKey
= this.findFreeWorkerNodeKey()
49 if (freeWorkerNodeKey
!== -1) {
50 return freeWorkerNodeKey
52 let minRunTime
= Infinity
53 let lessBusyWorkerNodeKey
!: number
54 for (const [index
, workerNode
] of this.pool
.workerNodes
.entries()) {
55 const workerRunTime
= workerNode
.tasksUsage
.runTime
56 if (workerRunTime
=== 0) {
58 } else if (workerRunTime
< minRunTime
) {
59 minRunTime
= workerRunTime
60 lessBusyWorkerNodeKey
= index
63 return lessBusyWorkerNodeKey
67 public remove (workerNodeKey
: number): boolean {