chore(deps-dev): bump tatami-ng to 0.6.0
[poolifier.git] / src / pools / selection-strategies / least-elu-worker-choice-strategy.ts
1 import type { IPool } from '../pool.js'
2 import type { IWorker } from '../worker.js'
3 import type {
4 IWorkerChoiceStrategy,
5 TaskStatisticsRequirements,
6 WorkerChoiceStrategyOptions,
7 } from './selection-strategies-types.js'
8
9 import { DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS } from '../utils.js'
10 import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy.js'
11
12 /**
13 * Selects the worker with the least ELU.
14 * @typeParam Worker - Type of worker which manages the strategy.
15 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
16 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
17 */
18 export 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 override readonly taskStatisticsRequirements: TaskStatisticsRequirements =
27 {
28 elu: {
29 aggregate: true,
30 average: false,
31 median: false,
32 },
33 runTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
34 waitTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
35 }
36
37 /** @inheritDoc */
38 public constructor (
39 pool: IPool<Worker, Data, Response>,
40 opts?: WorkerChoiceStrategyOptions
41 ) {
42 super(pool, opts)
43 this.setTaskStatisticsRequirements(this.opts)
44 }
45
46 private leastEluNextWorkerNodeKey (): number | undefined {
47 return this.pool.workerNodes.reduce(
48 (minWorkerNodeKey, workerNode, workerNodeKey, workerNodes) => {
49 return this.isWorkerNodeReady(workerNodeKey) &&
50 (workerNode.usage.elu.active.aggregate ?? 0) <
51 (workerNodes[minWorkerNodeKey].usage.elu.active.aggregate ?? 0)
52 ? workerNodeKey
53 : minWorkerNodeKey
54 },
55 0
56 )
57 }
58
59 /** @inheritDoc */
60 public choose (): number | undefined {
61 this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey)
62 this.nextWorkerNodeKey = this.leastEluNextWorkerNodeKey()
63 return this.nextWorkerNodeKey
64 }
65
66 /** @inheritDoc */
67 public remove (): boolean {
68 return true
69 }
70
71 /** @inheritDoc */
72 public reset (): boolean {
73 return true
74 }
75
76 /** @inheritDoc */
77 public update (): boolean {
78 return true
79 }
80 }