Commit | Line | Data |
---|---|---|
2fc5cae3 JB |
1 | import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils' |
2 | import type { IPool } from '../pool' | |
f06e48d8 | 3 | import type { IWorker } from '../worker' |
bdaf31cd | 4 | import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy' |
2fc5cae3 JB |
5 | import type { |
6 | IWorkerChoiceStrategy, | |
6c6afb84 | 7 | StrategyPolicy, |
2fc5cae3 JB |
8 | WorkerChoiceStrategyOptions |
9 | } from './selection-strategies-types' | |
bdaf31cd JB |
10 | |
11 | /** | |
12 | * Selects the next worker in a round robin fashion. | |
13 | * | |
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. | |
bdaf31cd JB |
17 | */ |
18 | export class RoundRobinWorkerChoiceStrategy< | |
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 { |
6c6afb84 JB |
25 | /** @inheritDoc */ |
26 | public readonly strategyPolicy: StrategyPolicy = { | |
94407def | 27 | dynamicWorkerUsage: false, |
b1aae695 | 28 | dynamicWorkerReady: true |
6c6afb84 JB |
29 | } |
30 | ||
2fc5cae3 JB |
31 | /** @inheritDoc */ |
32 | public constructor ( | |
33 | pool: IPool<Worker, Data, Response>, | |
34 | opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS | |
35 | ) { | |
36 | super(pool, opts) | |
932fc8be | 37 | this.setTaskStatisticsRequirements(this.opts) |
2fc5cae3 JB |
38 | } |
39 | ||
afc003b2 | 40 | /** @inheritDoc */ |
a6f7f1b4 | 41 | public reset (): boolean { |
9b106837 | 42 | this.nextWorkerNodeKey = 0 |
ea7a90d3 JB |
43 | return true |
44 | } | |
45 | ||
138d29a8 JB |
46 | /** @inheritDoc */ |
47 | public update (): boolean { | |
48 | return true | |
49 | } | |
50 | ||
afc003b2 | 51 | /** @inheritDoc */ |
b1aae695 | 52 | public choose (): number | undefined { |
9b106837 | 53 | const chosenWorkerNodeKey = this.nextWorkerNodeKey |
b1aae695 JB |
54 | this.roundRobinNextWorkerNodeKey() |
55 | if (!this.isWorkerNodeEligible(this.nextWorkerNodeKey as number)) { | |
56 | this.nextWorkerNodeKey = undefined | |
57 | } | |
f06e48d8 | 58 | return chosenWorkerNodeKey |
bdaf31cd | 59 | } |
97a2abc3 | 60 | |
afc003b2 | 61 | /** @inheritDoc */ |
f06e48d8 | 62 | public remove (workerNodeKey: number): boolean { |
9b106837 | 63 | if (this.nextWorkerNodeKey === workerNodeKey) { |
f06e48d8 | 64 | if (this.pool.workerNodes.length === 0) { |
9b106837 JB |
65 | this.nextWorkerNodeKey = 0 |
66 | } else if (this.nextWorkerNodeKey > this.pool.workerNodes.length - 1) { | |
67 | this.nextWorkerNodeKey = this.pool.workerNodes.length - 1 | |
78ab2555 | 68 | } |
97a2abc3 JB |
69 | } |
70 | return true | |
71 | } | |
9b106837 | 72 | |
b1aae695 | 73 | private roundRobinNextWorkerNodeKey (): number | undefined { |
9b106837 JB |
74 | this.nextWorkerNodeKey = |
75 | this.nextWorkerNodeKey === this.pool.workerNodes.length - 1 | |
76 | ? 0 | |
b1aae695 | 77 | : (this.nextWorkerNodeKey ?? 0) + 1 |
20016c79 | 78 | return this.nextWorkerNodeKey |
9b106837 | 79 | } |
bdaf31cd | 80 | } |