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'
7 WorkerChoiceStrategyOptions
8 } from
'./selection-strategies-types'
11 * Selects the next worker in a round robin fashion.
13 * @typeParam Worker - Type of worker which manages the strategy.
14 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
15 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
17 export class RoundRobinWorkerChoiceStrategy
<
18 Worker
extends IWorker
,
22 extends AbstractWorkerChoiceStrategy
<Worker
, Data
, Response
>
23 implements IWorkerChoiceStrategy
{
26 pool
: IPool
<Worker
, Data
, Response
>,
27 opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
30 this.setTaskStatisticsRequirements(this.opts
)
34 public reset (): boolean {
35 this.resetWorkerNodeKeyProperties()
40 public update (): boolean {
45 public choose (): number | undefined {
46 const chosenWorkerNodeKey
= this.nextWorkerNodeKey
47 this.roundRobinNextWorkerNodeKey()
48 this.checkNextWorkerNodeEligibility(chosenWorkerNodeKey
)
49 return chosenWorkerNodeKey
53 public remove (workerNodeKey
: number): boolean {
54 if (this.nextWorkerNodeKey
=== workerNodeKey
) {
55 if (this.pool
.workerNodes
.length
=== 0) {
56 this.nextWorkerNodeKey
= 0
57 } else if (this.nextWorkerNodeKey
> this.pool
.workerNodes
.length
- 1) {
58 this.nextWorkerNodeKey
= this.pool
.workerNodes
.length
- 1
64 private roundRobinNextWorkerNodeKey (): number | undefined {
65 this.nextWorkerNodeKey
=
66 this.nextWorkerNodeKey
=== this.pool
.workerNodes
.length
- 1
68 : (this.nextWorkerNodeKey
?? this.previousWorkerNodeKey
) + 1
69 return this.nextWorkerNodeKey