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 least 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 LeastBusyWorkerChoiceStrategy
<
19 Worker
extends IWorker
,
23 extends AbstractWorkerChoiceStrategy
<Worker
, Data
, Response
>
24 implements IWorkerChoiceStrategy
{
26 public readonly taskStatistics
: TaskStatistics
= {
38 pool
: IPool
<Worker
, Data
, Response
>,
39 opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
42 this.setTaskStatistics(this.opts
)
46 public reset (): boolean {
51 public update (): boolean {
56 public choose (): number {
57 let minRunTime
= Infinity
58 let leastBusyWorkerNodeKey
!: number
59 for (const [workerNodeKey
, workerNode
] of this.pool
.workerNodes
.entries()) {
60 const workerRunTime
= workerNode
.tasksUsage
.runTime
61 if (workerRunTime
=== 0) {
63 } else if (workerRunTime
< minRunTime
) {
64 minRunTime
= workerRunTime
65 leastBusyWorkerNodeKey
= workerNodeKey
68 return leastBusyWorkerNodeKey
72 public remove (): boolean {