1 import type { IPool
} from
'../pool.js'
2 import type { IWorker
} from
'../worker.js'
3 import { AbstractWorkerChoiceStrategy
} from
'./abstract-worker-choice-strategy.js'
6 WorkerChoiceStrategyOptions
,
7 } from
'./selection-strategies-types.js'
10 * Selects the next worker in a round robin fashion.
11 * @typeParam Worker - Type of worker which manages the strategy.
12 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
13 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
15 export class RoundRobinWorkerChoiceStrategy
<
16 Worker
extends IWorker
,
20 extends AbstractWorkerChoiceStrategy
<Worker
, Data
, Response
>
21 implements IWorkerChoiceStrategy
{
24 pool
: IPool
<Worker
, Data
, Response
>,
25 opts
?: WorkerChoiceStrategyOptions
31 public reset (): boolean {
32 this.resetWorkerNodeKeyProperties()
37 public update (): boolean {
42 public choose (): number | undefined {
43 const chosenWorkerNodeKey
= this.nextWorkerNodeKey
44 this.setPreviousWorkerNodeKey(chosenWorkerNodeKey
)
45 this.roundRobinNextWorkerNodeKey()
46 this.checkNextWorkerNodeKey()
47 return chosenWorkerNodeKey
51 public remove (workerNodeKey
: number): boolean {
52 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