1 import type { IWorker
} from
'../worker'
2 import { AbstractWorkerChoiceStrategy
} from
'./abstract-worker-choice-strategy'
6 } from
'./selection-strategies-types'
9 * Selects the less busy worker.
11 * @typeParam Worker - Type of worker which manages the strategy.
12 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
13 * @typeParam Response - Type of response of execution. This can only be serializable data.
15 export class LessBusyWorkerChoiceStrategy
<
16 Worker
extends IWorker
,
20 extends AbstractWorkerChoiceStrategy
<Worker
, Data
, Response
>
21 implements IWorkerChoiceStrategy
{
23 public readonly requiredStatistics
: RequiredStatistics
= {
30 public reset (): boolean {
35 public choose (): number {
36 const freeWorkerNodeKey
= this.pool
.findFreeWorkerNodeKey()
37 if (freeWorkerNodeKey
!== -1) {
38 return freeWorkerNodeKey
40 let minRunTime
= Infinity
41 let lessBusyWorkerNodeKey
!: number
42 for (const [index
, workerNode
] of this.pool
.workerNodes
.entries()) {
43 const workerRunTime
= workerNode
.tasksUsage
.runTime
44 if (workerRunTime
=== 0) {
46 } else if (workerRunTime
< minRunTime
) {
47 minRunTime
= workerRunTime
48 lessBusyWorkerNodeKey
= index
51 return lessBusyWorkerNodeKey
55 public remove (workerNodeKey
: number): boolean {