refactor: cleanup eslint configuration
[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
JB
7 TaskStatisticsRequirements,
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.
14 *
38e795c1 15 * @typeParam Worker - Type of worker which manages the strategy.
e102732c
JB
16 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
17 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
b3432a63
JB
18 */
19export class WeightedRoundRobinWorkerChoiceStrategy<
f06e48d8 20 Worker extends IWorker,
b2b1d84e
JB
21 Data = unknown,
22 Response = unknown
bf90656c
JB
23 >
24 extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
17393ac8 25 implements IWorkerChoiceStrategy {
afc003b2 26 /** @inheritDoc */
87de9ff5 27 public readonly taskStatisticsRequirements: TaskStatisticsRequirements = {
932fc8be
JB
28 runTime: {
29 aggregate: true,
30 average: true,
31 median: false
32 },
3c93feb9
JB
33 waitTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
34 elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
10fcfaf4
JB
35 }
36
b3432a63 37 /**
f3a91bac 38 * Worker node virtual task runtime.
b3432a63 39 */
c63a35a0 40 private workerNodeVirtualTaskRunTime = 0
b3432a63 41
2fc5cae3 42 /** @inheritDoc */
da309861 43 public constructor (
c4855468 44 pool: IPool<Worker, Data, Response>,
39618ede 45 opts?: WorkerChoiceStrategyOptions
da309861
JB
46 ) {
47 super(pool, opts)
050477bd 48 this.setTaskStatisticsRequirements(this.opts)
b3432a63
JB
49 }
50
afc003b2 51 /** @inheritDoc */
a6f7f1b4 52 public reset (): boolean {
39a43af7 53 this.resetWorkerNodeKeyProperties()
f3a91bac 54 this.workerNodeVirtualTaskRunTime = 0
ea7a90d3
JB
55 return true
56 }
57
138d29a8
JB
58 /** @inheritDoc */
59 public update (): boolean {
60 return true
61 }
62
afc003b2 63 /** @inheritDoc */
b1aae695 64 public choose (): number | undefined {
baca80f7 65 this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey)
a38b62f1 66 this.weightedRoundRobinNextWorkerNodeKey()
8e8d9101 67 this.checkNextWorkerNodeKey()
a38b62f1 68 return this.nextWorkerNodeKey
b3432a63
JB
69 }
70
afc003b2 71 /** @inheritDoc */
f06e48d8 72 public remove (workerNodeKey: number): boolean {
226b02a3
JB
73 if (this.pool.workerNodes.length === 0) {
74 this.reset()
153179f2 75 return true
226b02a3 76 }
9b106837 77 if (this.nextWorkerNodeKey === workerNodeKey) {
f3a91bac 78 this.workerNodeVirtualTaskRunTime = 0
0dd90fe1
JB
79 if (this.nextWorkerNodeKey > this.pool.workerNodes.length - 1) {
80 this.nextWorkerNodeKey = this.pool.workerNodes.length - 1
81 }
97a2abc3 82 }
226b02a3
JB
83 if (
84 this.previousWorkerNodeKey === workerNodeKey &&
85 this.previousWorkerNodeKey > this.pool.workerNodes.length - 1
86 ) {
87 this.previousWorkerNodeKey = this.pool.workerNodes.length - 1
88 }
08f3f44c 89 return true
2377984d 90 }
9b106837 91
b1aae695 92 private weightedRoundRobinNextWorkerNodeKey (): number | undefined {
67f3f2d6
JB
93 const workerWeight =
94 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
52738541 95 this.opts!.weights![this.nextWorkerNodeKey ?? this.previousWorkerNodeKey]
a38b62f1
JB
96 if (this.workerNodeVirtualTaskRunTime < workerWeight) {
97 this.workerNodeVirtualTaskRunTime =
98 this.workerNodeVirtualTaskRunTime +
99 this.getWorkerNodeTaskRunTime(
7c7bb289 100 this.nextWorkerNodeKey ?? this.previousWorkerNodeKey
a38b62f1
JB
101 )
102 } else {
103 this.nextWorkerNodeKey =
104 this.nextWorkerNodeKey === this.pool.workerNodes.length - 1
105 ? 0
106 : (this.nextWorkerNodeKey ?? this.previousWorkerNodeKey) + 1
107 this.workerNodeVirtualTaskRunTime = 0
108 }
20016c79 109 return this.nextWorkerNodeKey
9b106837 110 }
b3432a63 111}