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