1 import type { IPoolWorker
} from
'../pool-worker'
2 import { AbstractWorkerChoiceStrategy
} from
'./abstract-worker-choice-strategy'
3 import type { RequiredStatistics
} from
'./selection-strategies-types'
6 * Selects the less busy 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 LessBusyWorkerChoiceStrategy
<
13 Worker
extends IPoolWorker
,
16 > extends AbstractWorkerChoiceStrategy
<Worker
, Data
, Response
> {
18 public readonly requiredStatistics
: RequiredStatistics
= {
23 public reset (): boolean {
28 public choose (): number {
29 let minRunTime
= Infinity
30 let lessBusyWorkerKey
!: number
31 for (const [index
, workerItem
] of this.pool
.workers
.entries()) {
32 const workerRunTime
= workerItem
.tasksUsage
.runTime
33 if (!this.isDynamicPool
&& workerRunTime
=== 0) {
35 } else if (workerRunTime
< minRunTime
) {
36 minRunTime
= workerRunTime
37 lessBusyWorkerKey
= index
40 return lessBusyWorkerKey
44 public remove (workerKey
: number): boolean {