1 import type { IPool
} from
'../pool.js'
2 import type { IWorker
} from
'../worker.js'
5 WorkerChoiceStrategyOptions
,
6 } from
'./selection-strategies-types.js'
8 import { AbstractWorkerChoiceStrategy
} from
'./abstract-worker-choice-strategy.js'
11 * 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
?: WorkerChoiceStrategyOptions
31 private roundRobinNextWorkerNodeKey (): number | undefined {
32 this.nextWorkerNodeKey
=
33 this.nextWorkerNodeKey
=== this.pool
.workerNodes
.length
- 1
35 : (this.nextWorkerNodeKey
?? this.previousWorkerNodeKey
) + 1
36 return this.nextWorkerNodeKey
40 public choose (): number | undefined {
41 const chosenWorkerNodeKey
= this.nextWorkerNodeKey
42 this.setPreviousWorkerNodeKey(chosenWorkerNodeKey
)
43 this.roundRobinNextWorkerNodeKey()
44 this.checkNextWorkerNodeKey()
45 return chosenWorkerNodeKey
49 public remove (workerNodeKey
: number): boolean {
50 if (this.pool
.workerNodes
.length
=== 0) {
55 this.nextWorkerNodeKey
=== workerNodeKey
&&
56 this.nextWorkerNodeKey
> this.pool
.workerNodes
.length
- 1
58 this.nextWorkerNodeKey
= this.pool
.workerNodes
.length
- 1
61 this.previousWorkerNodeKey
=== workerNodeKey
&&
62 this.previousWorkerNodeKey
> this.pool
.workerNodes
.length
- 1
64 this.previousWorkerNodeKey
= this.pool
.workerNodes
.length
- 1
70 public reset (): boolean {
71 this.resetWorkerNodeKeyProperties()
76 public update (): boolean {