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 JB |
14 | * @typeParam Worker - Type of worker which manages the strategy. |
15 | * @typeParam Data - Type of data sent to the worker. This can only be serializable data. | |
02706357 | 16 | * @typeParam Response - Type of execution response. This can only be serializable 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 = { | |
27 | useDynamicWorker: true | |
28 | } | |
29 | ||
bdaf31cd | 30 | /** |
f06e48d8 | 31 | * Id of the next worker node. |
bdaf31cd | 32 | */ |
f06e48d8 | 33 | private nextWorkerNodeId: number = 0 |
bdaf31cd | 34 | |
2fc5cae3 JB |
35 | /** @inheritDoc */ |
36 | public constructor ( | |
37 | pool: IPool<Worker, Data, Response>, | |
38 | opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS | |
39 | ) { | |
40 | super(pool, opts) | |
932fc8be | 41 | this.setTaskStatisticsRequirements(this.opts) |
2fc5cae3 JB |
42 | } |
43 | ||
afc003b2 | 44 | /** @inheritDoc */ |
a6f7f1b4 | 45 | public reset (): boolean { |
f06e48d8 | 46 | this.nextWorkerNodeId = 0 |
ea7a90d3 JB |
47 | return true |
48 | } | |
49 | ||
138d29a8 JB |
50 | /** @inheritDoc */ |
51 | public update (): boolean { | |
52 | return true | |
53 | } | |
54 | ||
afc003b2 | 55 | /** @inheritDoc */ |
c923ce56 | 56 | public choose (): number { |
f06e48d8 JB |
57 | const chosenWorkerNodeKey = this.nextWorkerNodeId |
58 | this.nextWorkerNodeId = | |
59 | this.nextWorkerNodeId === this.pool.workerNodes.length - 1 | |
bdaf31cd | 60 | ? 0 |
f06e48d8 JB |
61 | : this.nextWorkerNodeId + 1 |
62 | return chosenWorkerNodeKey | |
bdaf31cd | 63 | } |
97a2abc3 | 64 | |
afc003b2 | 65 | /** @inheritDoc */ |
f06e48d8 JB |
66 | public remove (workerNodeKey: number): boolean { |
67 | if (this.nextWorkerNodeId === workerNodeKey) { | |
68 | if (this.pool.workerNodes.length === 0) { | |
69 | this.nextWorkerNodeId = 0 | |
97f4fd90 JB |
70 | } else if (this.nextWorkerNodeId > this.pool.workerNodes.length - 1) { |
71 | this.nextWorkerNodeId = this.pool.workerNodes.length - 1 | |
78ab2555 | 72 | } |
97a2abc3 JB |
73 | } |
74 | return true | |
75 | } | |
bdaf31cd | 76 | } |