fix: fix build after merge with main
[poolifier.git] / src / pools / selection-strategies / least-elu-worker-choice-strategy.ts
CommitLineData
058a9457
JB
1import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils'
2import type { IPool } from '../pool'
3import type { IWorker } from '../worker'
4import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
5import type {
6 IWorkerChoiceStrategy,
7 TaskStatistics,
8 WorkerChoiceStrategyOptions
9} from './selection-strategies-types'
10
11/**
12 * Selects the worker with the least ELU.
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.
16 * @typeParam Response - Type of execution response. This can only be serializable data.
17 */
18export class LeastEluWorkerChoiceStrategy<
19 Worker extends IWorker,
20 Data = unknown,
21 Response = unknown
22 >
23 extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
24 implements IWorkerChoiceStrategy {
25 /** @inheritDoc */
26 public readonly taskStatistics: TaskStatistics = {
27 runTime: false,
28 avgRunTime: true,
29 medRunTime: false,
30 waitTime: false,
31 avgWaitTime: false,
32 medWaitTime: false,
33 elu: true
34 }
35
36 /** @inheritDoc */
37 public constructor (
38 pool: IPool<Worker, Data, Response>,
39 opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
40 ) {
41 super(pool, opts)
42 this.setTaskStatistics(this.opts)
43 }
44
45 /** @inheritDoc */
46 public reset (): boolean {
47 return true
48 }
49
50 /** @inheritDoc */
51 public update (): boolean {
52 return true
53 }
54
55 /** @inheritDoc */
56 public choose (): number {
cdb517b3 57 let minWorkerElu = Infinity
058a9457
JB
58 let leastEluWorkerNodeKey!: number
59 for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) {
cdb517b3
JB
60 const workerUsage = workerNode.workerUsage
61 const workerElu = workerUsage.elu?.utilization ?? 0
62 if (workerElu === 0) {
058a9457 63 return workerNodeKey
cdb517b3
JB
64 } else if (workerElu < minWorkerElu) {
65 minWorkerElu = workerElu
058a9457
JB
66 leastEluWorkerNodeKey = workerNodeKey
67 }
68 }
69 return leastEluWorkerNodeKey
70 }
71
72 /** @inheritDoc */
73 public remove (): boolean {
74 return true
75 }
76}