docs: fix typedoc generation with inheritance
[poolifier.git] / src / pools / selection-strategies / less-used-worker-choice-strategy.ts
CommitLineData
ea7a90d3 1import type { IPoolWorker } from '../pool-worker'
bdaf31cd 2import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
bf90656c 3import type { IWorkerChoiceStrategy } from './selection-strategies-types'
bdaf31cd
JB
4
5/**
737c6d97 6 * Selects the less used worker.
bdaf31cd 7 *
38e795c1
JB
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.
bdaf31cd 11 */
737c6d97 12export class LessUsedWorkerChoiceStrategy<
bf90656c 13 Worker extends IPoolWorker,
b2b1d84e
JB
14 Data = unknown,
15 Response = unknown
bf90656c
JB
16 >
17 extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
3300e7bc 18 implements IWorkerChoiceStrategy<Worker, Data, Response> {
afc003b2 19 /** @inheritDoc */
a6f7f1b4 20 public reset (): boolean {
ea7a90d3
JB
21 return true
22 }
23
afc003b2 24 /** @inheritDoc */
c923ce56 25 public choose (): number {
c141008c 26 const freeWorkerKey = this.pool.findFreeWorkerKey()
965415bb 27 if (freeWorkerKey !== -1) {
c141008c
JB
28 return freeWorkerKey
29 }
f4ff1ce2 30 let minNumberOfTasks = Infinity
c923ce56
JB
31 let lessUsedWorkerKey!: number
32 for (const [index, workerItem] of this.pool.workers.entries()) {
33 const tasksUsage = workerItem.tasksUsage
53cf3405 34 const workerTasks = tasksUsage.run + tasksUsage.running
cf9c7b65 35 if (workerTasks === 0) {
c923ce56 36 return index
f4ff1ce2
JB
37 } else if (workerTasks < minNumberOfTasks) {
38 minNumberOfTasks = workerTasks
c923ce56 39 lessUsedWorkerKey = index
bdaf31cd
JB
40 }
41 }
c923ce56 42 return lessUsedWorkerKey
bdaf31cd 43 }
97a2abc3 44
afc003b2 45 /** @inheritDoc */
97a2abc3
JB
46 public remove (workerKey: number): boolean {
47 return true
48 }
bdaf31cd 49}