Commit | Line | Data |
---|---|---|
ea7a90d3 | 1 | import type { IPoolWorker } from '../pool-worker' |
bdaf31cd JB |
2 | import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy' |
3 | ||
4 | /** | |
737c6d97 | 5 | * Selects the less used worker. |
bdaf31cd | 6 | * |
38e795c1 JB |
7 | * @typeParam Worker - Type of worker which manages the strategy. |
8 | * @typeParam Data - Type of data sent to the worker. This can only be serializable data. | |
9 | * @typeParam Response - Type of response of execution. This can only be serializable data. | |
bdaf31cd | 10 | */ |
737c6d97 | 11 | export class LessUsedWorkerChoiceStrategy< |
ea7a90d3 | 12 | Worker extends IPoolWorker, |
bdaf31cd JB |
13 | Data, |
14 | Response | |
15 | > extends AbstractWorkerChoiceStrategy<Worker, Data, Response> { | |
38e795c1 | 16 | /** {@inheritDoc} */ |
a6f7f1b4 | 17 | public reset (): boolean { |
ea7a90d3 JB |
18 | return true |
19 | } | |
20 | ||
38e795c1 | 21 | /** {@inheritDoc} */ |
c923ce56 | 22 | public choose (): number { |
f4ff1ce2 | 23 | let minNumberOfTasks = Infinity |
c923ce56 JB |
24 | let lessUsedWorkerKey!: number |
25 | for (const [index, workerItem] of this.pool.workers.entries()) { | |
26 | const tasksUsage = workerItem.tasksUsage | |
27 | const workerTasks = tasksUsage?.run + tasksUsage?.running | |
f4ff1ce2 | 28 | if (!this.isDynamicPool && workerTasks === 0) { |
c923ce56 | 29 | return index |
f4ff1ce2 JB |
30 | } else if (workerTasks < minNumberOfTasks) { |
31 | minNumberOfTasks = workerTasks | |
c923ce56 | 32 | lessUsedWorkerKey = index |
bdaf31cd JB |
33 | } |
34 | } | |
c923ce56 | 35 | return lessUsedWorkerKey |
bdaf31cd | 36 | } |
97a2abc3 JB |
37 | |
38 | /** {@inheritDoc} */ | |
39 | public remove (workerKey: number): boolean { | |
40 | return true | |
41 | } | |
bdaf31cd | 42 | } |