build: fix eslint configuration with type checking
[poolifier.git] / src / pools / selection-strategies / weighted-round-robin-worker-choice-strategy.ts
CommitLineData
d35e5717 1import type { IPool } from '../pool.js'
e9ed6eee 2import { DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS } from '../utils.js'
ded253e2 3import type { IWorker } from '../worker.js'
d35e5717 4import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy.js'
bf90656c
JB
5import type {
6 IWorkerChoiceStrategy,
39618ede 7 TaskStatisticsRequirements,
3a502712 8 WorkerChoiceStrategyOptions,
d35e5717 9} from './selection-strategies-types.js'
b3432a63 10
b3432a63
JB
11/**
12 * Selects the next worker with a weighted round robin scheduling algorithm.
13 * Loosely modeled after the weighted round robin queueing algorithm: https://en.wikipedia.org/wiki/Weighted_round_robin.
38e795c1 14 * @typeParam Worker - Type of worker which manages the strategy.
e102732c
JB
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.
b3432a63
JB
17 */
18export class WeightedRoundRobinWorkerChoiceStrategy<
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 = {
932fc8be
JB
27 runTime: {
28 aggregate: true,
29 average: true,
3a502712 30 median: false,
932fc8be 31 },
e0843544
JB
32 waitTime: {
33 aggregate: true,
34 average: true,
3a502712 35 median: false,
e0843544 36 },
3a502712 37 elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
10fcfaf4
JB
38 }
39
b3432a63 40 /**
e0843544 41 * Worker node virtual execution time.
b3432a63 42 */
e0843544 43 private workerNodeVirtualTaskExecutionTime = 0
b3432a63 44
2fc5cae3 45 /** @inheritDoc */
da309861 46 public constructor (
c4855468 47 pool: IPool<Worker, Data, Response>,
39618ede 48 opts?: WorkerChoiceStrategyOptions
da309861
JB
49 ) {
50 super(pool, opts)
050477bd 51 this.setTaskStatisticsRequirements(this.opts)
b3432a63
JB
52 }
53
afc003b2 54 /** @inheritDoc */
a6f7f1b4 55 public reset (): boolean {
39a43af7 56 this.resetWorkerNodeKeyProperties()
e0843544 57 this.workerNodeVirtualTaskExecutionTime = 0
ea7a90d3
JB
58 return true
59 }
60
138d29a8
JB
61 /** @inheritDoc */
62 public update (): boolean {
63 return true
64 }
65
afc003b2 66 /** @inheritDoc */
b1aae695 67 public choose (): number | undefined {
baca80f7 68 this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey)
a38b62f1 69 this.weightedRoundRobinNextWorkerNodeKey()
8e8d9101 70 this.checkNextWorkerNodeKey()
a38b62f1 71 return this.nextWorkerNodeKey
b3432a63
JB
72 }
73
afc003b2 74 /** @inheritDoc */
f06e48d8 75 public remove (workerNodeKey: number): boolean {
226b02a3
JB
76 if (this.pool.workerNodes.length === 0) {
77 this.reset()
153179f2 78 return true
226b02a3 79 }
9b106837 80 if (this.nextWorkerNodeKey === workerNodeKey) {
e0843544 81 this.workerNodeVirtualTaskExecutionTime = 0
0dd90fe1
JB
82 if (this.nextWorkerNodeKey > this.pool.workerNodes.length - 1) {
83 this.nextWorkerNodeKey = this.pool.workerNodes.length - 1
84 }
97a2abc3 85 }
226b02a3
JB
86 if (
87 this.previousWorkerNodeKey === workerNodeKey &&
88 this.previousWorkerNodeKey > this.pool.workerNodes.length - 1
89 ) {
90 this.previousWorkerNodeKey = this.pool.workerNodes.length - 1
91 }
08f3f44c 92 return true
2377984d 93 }
9b106837 94
b1aae695 95 private weightedRoundRobinNextWorkerNodeKey (): number | undefined {
67f3f2d6
JB
96 const workerWeight =
97 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
52738541 98 this.opts!.weights![this.nextWorkerNodeKey ?? this.previousWorkerNodeKey]
e0843544
JB
99 if (this.workerNodeVirtualTaskExecutionTime < workerWeight) {
100 this.workerNodeVirtualTaskExecutionTime +=
101 this.getWorkerNodeTaskWaitTime(
102 this.nextWorkerNodeKey ?? this.previousWorkerNodeKey
103 ) +
a38b62f1 104 this.getWorkerNodeTaskRunTime(
7c7bb289 105 this.nextWorkerNodeKey ?? this.previousWorkerNodeKey
a38b62f1
JB
106 )
107 } else {
108 this.nextWorkerNodeKey =
109 this.nextWorkerNodeKey === this.pool.workerNodes.length - 1
110 ? 0
111 : (this.nextWorkerNodeKey ?? this.previousWorkerNodeKey) + 1
e0843544 112 this.workerNodeVirtualTaskExecutionTime = 0
a38b62f1 113 }
20016c79 114 return this.nextWorkerNodeKey
9b106837 115 }
b3432a63 116}