build: update volta node version
[poolifier.git] / src / pools / selection-strategies / least-busy-worker-choice-strategy.ts
CommitLineData
2fc5cae3
JB
1import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils'
2import type { IPool } from '../pool'
f06e48d8 3import type { IWorker } from '../worker'
168c526f 4import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
bf90656c
JB
5import type {
6 IWorkerChoiceStrategy,
87de9ff5 7 TaskStatisticsRequirements,
2fc5cae3 8 WorkerChoiceStrategyOptions
bf90656c 9} from './selection-strategies-types'
168c526f
JB
10
11/**
e4543b14 12 * Selects the least busy worker.
168c526f
JB
13 *
14 * @typeParam Worker - Type of worker which manages the strategy.
15 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
02706357 16 * @typeParam Response - Type of execution response. This can only be serializable data.
168c526f 17 */
e4543b14 18export class LeastBusyWorkerChoiceStrategy<
f06e48d8 19 Worker extends IWorker,
b2b1d84e
JB
20 Data = unknown,
21 Response = unknown
bf90656c
JB
22 >
23 extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
17393ac8 24 implements IWorkerChoiceStrategy {
afc003b2 25 /** @inheritDoc */
87de9ff5 26 public readonly taskStatisticsRequirements: TaskStatisticsRequirements = {
c6bd2650 27 runTime: true,
78099a15 28 avgRunTime: false,
0567595a 29 medRunTime: false,
1c6fe997 30 waitTime: true,
0567595a 31 avgWaitTime: false,
62c15a68
JB
32 medWaitTime: false,
33 elu: false
168c526f
JB
34 }
35
2fc5cae3
JB
36 /** @inheritDoc */
37 public constructor (
38 pool: IPool<Worker, Data, Response>,
39 opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
40 ) {
41 super(pool, opts)
b6b32453 42 this.setTaskStatistics(this.opts)
2fc5cae3
JB
43 }
44
afc003b2 45 /** @inheritDoc */
168c526f
JB
46 public reset (): boolean {
47 return true
48 }
49
138d29a8
JB
50 /** @inheritDoc */
51 public update (): boolean {
52 return true
53 }
54
afc003b2 55 /** @inheritDoc */
c923ce56 56 public choose (): number {
1c6fe997 57 let minTime = Infinity
e4543b14 58 let leastBusyWorkerNodeKey!: number
08f3f44c 59 for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) {
1c6fe997
JB
60 const workerTime =
61 workerNode.workerUsage.runTime.aggregation +
62 workerNode.workerUsage.waitTime.aggregation
63 if (workerTime === 0) {
08f3f44c 64 return workerNodeKey
1c6fe997
JB
65 } else if (workerTime < minTime) {
66 minTime = workerTime
e4543b14 67 leastBusyWorkerNodeKey = workerNodeKey
168c526f
JB
68 }
69 }
e4543b14 70 return leastBusyWorkerNodeKey
168c526f 71 }
97a2abc3 72
afc003b2 73 /** @inheritDoc */
a4958de2 74 public remove (): boolean {
97a2abc3
JB
75 return true
76 }
168c526f 77}