fix: refine possible null exception fix at task response handling
[poolifier.git] / src / pools / selection-strategies / round-robin-worker-choice-strategy.ts
CommitLineData
2fc5cae3
JB
1import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils'
2import type { IPool } from '../pool'
f06e48d8 3import type { IWorker } from '../worker'
bdaf31cd 4import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
2fc5cae3
JB
5import type {
6 IWorkerChoiceStrategy,
7 WorkerChoiceStrategyOptions
8} from './selection-strategies-types'
bdaf31cd
JB
9
10/**
11 * Selects the next worker in a round robin fashion.
12 *
38e795c1 13 * @typeParam Worker - Type of worker which manages the strategy.
e102732c
JB
14 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
15 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
bdaf31cd
JB
16 */
17export class RoundRobinWorkerChoiceStrategy<
f06e48d8 18 Worker extends IWorker,
b2b1d84e
JB
19 Data = unknown,
20 Response = unknown
bf90656c
JB
21 >
22 extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
17393ac8 23 implements IWorkerChoiceStrategy {
2fc5cae3
JB
24 /** @inheritDoc */
25 public constructor (
26 pool: IPool<Worker, Data, Response>,
27 opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
28 ) {
29 super(pool, opts)
932fc8be 30 this.setTaskStatisticsRequirements(this.opts)
2fc5cae3
JB
31 }
32
afc003b2 33 /** @inheritDoc */
a6f7f1b4 34 public reset (): boolean {
39a43af7 35 this.resetWorkerNodeKeyProperties()
ea7a90d3
JB
36 return true
37 }
38
138d29a8
JB
39 /** @inheritDoc */
40 public update (): boolean {
41 return true
42 }
43
afc003b2 44 /** @inheritDoc */
b1aae695 45 public choose (): number | undefined {
9b106837 46 const chosenWorkerNodeKey = this.nextWorkerNodeKey
baca80f7 47 this.setPreviousWorkerNodeKey(chosenWorkerNodeKey)
b1aae695 48 this.roundRobinNextWorkerNodeKey()
a38b62f1 49 this.checkNextWorkerNodeReadiness()
f06e48d8 50 return chosenWorkerNodeKey
bdaf31cd 51 }
97a2abc3 52
afc003b2 53 /** @inheritDoc */
f06e48d8 54 public remove (workerNodeKey: number): boolean {
226b02a3
JB
55 if (this.pool.workerNodes.length === 0) {
56 this.reset()
57 }
58 if (
59 this.nextWorkerNodeKey === workerNodeKey &&
60 this.nextWorkerNodeKey > this.pool.workerNodes.length - 1
61 ) {
62 this.nextWorkerNodeKey = this.pool.workerNodes.length - 1
63 }
64 if (
65 this.previousWorkerNodeKey === workerNodeKey &&
66 this.previousWorkerNodeKey > this.pool.workerNodes.length - 1
67 ) {
68 this.previousWorkerNodeKey = this.pool.workerNodes.length - 1
97a2abc3
JB
69 }
70 return true
71 }
9b106837 72
b1aae695 73 private roundRobinNextWorkerNodeKey (): number | undefined {
a38b62f1
JB
74 this.nextWorkerNodeKey =
75 this.nextWorkerNodeKey === this.pool.workerNodes.length - 1
76 ? 0
77 : (this.nextWorkerNodeKey ?? this.previousWorkerNodeKey) + 1
20016c79 78 return this.nextWorkerNodeKey
9b106837 79 }
bdaf31cd 80}