1 import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
} from
'../../utils'
2 import type { IPool
} from
'../pool'
3 import type { IWorker
} from
'../worker'
4 import { AbstractWorkerChoiceStrategy
} from
'./abstract-worker-choice-strategy'
8 WorkerChoiceStrategyOptions
9 } from
'./selection-strategies-types'
12 * Selects the next worker in a round robin fashion.
14 * @typeParam Worker - Type of worker which manages the strategy.
15 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
16 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
18 export class RoundRobinWorkerChoiceStrategy
<
19 Worker
extends IWorker
,
23 extends AbstractWorkerChoiceStrategy
<Worker
, Data
, Response
>
24 implements IWorkerChoiceStrategy
{
26 public readonly strategyPolicy
: StrategyPolicy
= {
27 useDynamicWorker
: true
32 pool
: IPool
<Worker
, Data
, Response
>,
33 opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
36 this.setTaskStatisticsRequirements(this.opts
)
40 public reset (): boolean {
41 this.nextWorkerNodeKey
= 0
46 public update (): boolean {
51 public choose (): number {
52 const chosenWorkerNodeKey
= this.nextWorkerNodeKey
54 this.roundRobinNextWorkerNodeKey()
55 } while (!this.isWorkerNodeReady(this.nextWorkerNodeKey
))
56 return chosenWorkerNodeKey
60 public remove (workerNodeKey
: number): boolean {
61 if (this.nextWorkerNodeKey
=== workerNodeKey
) {
62 if (this.pool
.workerNodes
.length
=== 0) {
63 this.nextWorkerNodeKey
= 0
64 } else if (this.nextWorkerNodeKey
> this.pool
.workerNodes
.length
- 1) {
65 this.nextWorkerNodeKey
= this.pool
.workerNodes
.length
- 1
71 private roundRobinNextWorkerNodeKey (): number {
72 this.nextWorkerNodeKey
=
73 this.nextWorkerNodeKey
=== this.pool
.workerNodes
.length
- 1
75 : this.nextWorkerNodeKey
+ 1
76 return this.nextWorkerNodeKey