Commit | Line | Data |
---|---|---|
d35e5717 JB |
1 | import type { IPool } from '../pool.js' |
2 | import type { IWorker } from '../worker.js' | |
3 | import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy.js' | |
2fc5cae3 JB |
4 | import type { |
5 | IWorkerChoiceStrategy, | |
26ce26ca | 6 | InternalWorkerChoiceStrategyOptions |
d35e5717 | 7 | } from './selection-strategies-types.js' |
bdaf31cd JB |
8 | |
9 | /** | |
10 | * Selects the next worker in a round robin fashion. | |
11 | * | |
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>, | |
26ce26ca | 26 | opts: InternalWorkerChoiceStrategyOptions |
2fc5cae3 JB |
27 | ) { |
28 | super(pool, opts) | |
2fc5cae3 JB |
29 | } |
30 | ||
afc003b2 | 31 | /** @inheritDoc */ |
a6f7f1b4 | 32 | public reset (): boolean { |
39a43af7 | 33 | this.resetWorkerNodeKeyProperties() |
ea7a90d3 JB |
34 | return true |
35 | } | |
36 | ||
138d29a8 JB |
37 | /** @inheritDoc */ |
38 | public update (): boolean { | |
39 | return true | |
40 | } | |
41 | ||
afc003b2 | 42 | /** @inheritDoc */ |
b1aae695 | 43 | public choose (): number | undefined { |
9b106837 | 44 | const chosenWorkerNodeKey = this.nextWorkerNodeKey |
baca80f7 | 45 | this.setPreviousWorkerNodeKey(chosenWorkerNodeKey) |
b1aae695 | 46 | this.roundRobinNextWorkerNodeKey() |
a38b62f1 | 47 | this.checkNextWorkerNodeReadiness() |
f06e48d8 | 48 | return chosenWorkerNodeKey |
bdaf31cd | 49 | } |
97a2abc3 | 50 | |
afc003b2 | 51 | /** @inheritDoc */ |
f06e48d8 | 52 | public remove (workerNodeKey: number): boolean { |
226b02a3 JB |
53 | if (this.pool.workerNodes.length === 0) { |
54 | this.reset() | |
55 | } | |
56 | if ( | |
57 | this.nextWorkerNodeKey === workerNodeKey && | |
58 | this.nextWorkerNodeKey > this.pool.workerNodes.length - 1 | |
59 | ) { | |
60 | this.nextWorkerNodeKey = this.pool.workerNodes.length - 1 | |
61 | } | |
62 | if ( | |
63 | this.previousWorkerNodeKey === workerNodeKey && | |
64 | this.previousWorkerNodeKey > this.pool.workerNodes.length - 1 | |
65 | ) { | |
66 | this.previousWorkerNodeKey = this.pool.workerNodes.length - 1 | |
97a2abc3 JB |
67 | } |
68 | return true | |
69 | } | |
9b106837 | 70 | |
b1aae695 | 71 | private roundRobinNextWorkerNodeKey (): number | undefined { |
a38b62f1 JB |
72 | this.nextWorkerNodeKey = |
73 | this.nextWorkerNodeKey === this.pool.workerNodes.length - 1 | |
74 | ? 0 | |
75 | : (this.nextWorkerNodeKey ?? this.previousWorkerNodeKey) + 1 | |
20016c79 | 76 | return this.nextWorkerNodeKey |
9b106837 | 77 | } |
bdaf31cd | 78 | } |