Commit | Line | Data |
---|---|---|
168c526f JB |
1 | import type { IPoolWorker } from '../pool-worker' |
2 | import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy' | |
3 | import type { RequiredStatistics } from './selection-strategies-types' | |
4 | ||
5 | /** | |
6 | * Selects the less busy worker. | |
7 | * | |
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. | |
11 | */ | |
12 | export class LessBusyWorkerChoiceStrategy< | |
13 | Worker extends IPoolWorker, | |
14 | Data, | |
15 | Response | |
16 | > extends AbstractWorkerChoiceStrategy<Worker, Data, Response> { | |
17 | /** {@inheritDoc} */ | |
18 | public readonly requiredStatistics: RequiredStatistics = { | |
19 | runTime: true | |
20 | } | |
21 | ||
22 | /** {@inheritDoc} */ | |
23 | public reset (): boolean { | |
24 | return true | |
25 | } | |
26 | ||
27 | /** {@inheritDoc} */ | |
c923ce56 | 28 | public choose (): number { |
168c526f | 29 | let minRunTime = Infinity |
c923ce56 JB |
30 | let lessBusyWorkerKey!: number |
31 | for (const [index, workerItem] of this.pool.workers.entries()) { | |
32 | const workerRunTime = workerItem.tasksUsage.runTime | |
cf9c7b65 | 33 | if (workerRunTime === 0) { |
c923ce56 | 34 | return index |
168c526f JB |
35 | } else if (workerRunTime < minRunTime) { |
36 | minRunTime = workerRunTime | |
c923ce56 | 37 | lessBusyWorkerKey = index |
168c526f JB |
38 | } |
39 | } | |
c923ce56 | 40 | return lessBusyWorkerKey |
168c526f | 41 | } |
97a2abc3 JB |
42 | |
43 | /** {@inheritDoc} */ | |
44 | public remove (workerKey: number): boolean { | |
45 | return true | |
46 | } | |
168c526f | 47 | } |