1 import type { IPool
} from
'../pool'
2 import type { IWorker
} from
'../worker'
3 import { AbstractWorkerChoiceStrategy
} from
'./abstract-worker-choice-strategy'
6 InternalWorkerChoiceStrategyOptions
7 } from
'./selection-strategies-types'
10 * Selects the next worker in a round robin fashion.
12 * @typeParam Worker - Type of worker which manages the strategy.
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.
16 export class RoundRobinWorkerChoiceStrategy
<
17 Worker
extends IWorker
,
21 extends AbstractWorkerChoiceStrategy
<Worker
, Data
, Response
>
22 implements IWorkerChoiceStrategy
{
25 pool
: IPool
<Worker
, Data
, Response
>,
26 opts
: InternalWorkerChoiceStrategyOptions
32 public reset (): boolean {
33 this.resetWorkerNodeKeyProperties()
38 public update (): boolean {
43 public choose (): number | undefined {
44 const chosenWorkerNodeKey
= this.nextWorkerNodeKey
45 this.setPreviousWorkerNodeKey(chosenWorkerNodeKey
)
46 this.roundRobinNextWorkerNodeKey()
47 this.checkNextWorkerNodeReadiness()
48 return chosenWorkerNodeKey
52 public remove (workerNodeKey
: number): boolean {
53 if (this.pool
.workerNodes
.length
=== 0) {
57 this.nextWorkerNodeKey
=== workerNodeKey
&&
58 this.nextWorkerNodeKey
> this.pool
.workerNodes
.length
- 1
60 this.nextWorkerNodeKey
= this.pool
.workerNodes
.length
- 1
63 this.previousWorkerNodeKey
=== workerNodeKey
&&
64 this.previousWorkerNodeKey
> this.pool
.workerNodes
.length
- 1
66 this.previousWorkerNodeKey
= this.pool
.workerNodes
.length
- 1
71 private roundRobinNextWorkerNodeKey (): number | undefined {
72 this.nextWorkerNodeKey
=
73 this.nextWorkerNodeKey
=== this.pool
.workerNodes
.length
- 1
75 : (this.nextWorkerNodeKey
?? this.previousWorkerNodeKey
) + 1
76 return this.nextWorkerNodeKey