build(deps-dev): apply updates
[poolifier.git] / src / pools / selection-strategies / weighted-round-robin-worker-choice-strategy.ts
1 import type { IPool } from '../pool.js'
2 import { DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS } from '../utils.js'
3 import type { IWorker } from '../worker.js'
4 import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy.js'
5 import type {
6 IWorkerChoiceStrategy,
7 TaskStatisticsRequirements,
8 WorkerChoiceStrategyOptions
9 } from './selection-strategies-types.js'
10
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 *
15 * @typeParam Worker - Type of worker which manages the strategy.
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.
18 */
19 export class WeightedRoundRobinWorkerChoiceStrategy<
20 Worker extends IWorker,
21 Data = unknown,
22 Response = unknown
23 >
24 extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
25 implements IWorkerChoiceStrategy {
26 /** @inheritDoc */
27 public readonly taskStatisticsRequirements: TaskStatisticsRequirements = {
28 runTime: {
29 aggregate: true,
30 average: true,
31 median: false
32 },
33 waitTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
34 elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
35 }
36
37 /**
38 * Worker node virtual task runtime.
39 */
40 private workerNodeVirtualTaskRunTime = 0
41
42 /** @inheritDoc */
43 public constructor (
44 pool: IPool<Worker, Data, Response>,
45 opts?: WorkerChoiceStrategyOptions
46 ) {
47 super(pool, opts)
48 this.setTaskStatisticsRequirements(this.opts)
49 }
50
51 /** @inheritDoc */
52 public reset (): boolean {
53 this.resetWorkerNodeKeyProperties()
54 this.workerNodeVirtualTaskRunTime = 0
55 return true
56 }
57
58 /** @inheritDoc */
59 public update (): boolean {
60 return true
61 }
62
63 /** @inheritDoc */
64 public choose (): number | undefined {
65 this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey)
66 this.weightedRoundRobinNextWorkerNodeKey()
67 this.checkNextWorkerNodeKey()
68 return this.nextWorkerNodeKey
69 }
70
71 /** @inheritDoc */
72 public remove (workerNodeKey: number): boolean {
73 if (this.pool.workerNodes.length === 0) {
74 this.reset()
75 return true
76 }
77 if (this.nextWorkerNodeKey === workerNodeKey) {
78 this.workerNodeVirtualTaskRunTime = 0
79 if (this.nextWorkerNodeKey > this.pool.workerNodes.length - 1) {
80 this.nextWorkerNodeKey = this.pool.workerNodes.length - 1
81 }
82 }
83 if (
84 this.previousWorkerNodeKey === workerNodeKey &&
85 this.previousWorkerNodeKey > this.pool.workerNodes.length - 1
86 ) {
87 this.previousWorkerNodeKey = this.pool.workerNodes.length - 1
88 }
89 return true
90 }
91
92 private weightedRoundRobinNextWorkerNodeKey (): number | undefined {
93 const workerWeight =
94 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
95 this.opts!.weights![this.nextWorkerNodeKey ?? this.previousWorkerNodeKey]
96 if (this.workerNodeVirtualTaskRunTime < workerWeight) {
97 this.workerNodeVirtualTaskRunTime =
98 this.workerNodeVirtualTaskRunTime +
99 this.getWorkerNodeTaskRunTime(
100 this.nextWorkerNodeKey ?? this.previousWorkerNodeKey
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 }
109 return this.nextWorkerNodeKey
110 }
111 }