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 serializable data.
16 * @typeParam Response - Type of execution response. This can only be serializable 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
31 * Id of the next worker node.
33 private nextWorkerNodeId
: number = 0
37 pool
: IPool
<Worker
, Data
, Response
>,
38 opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
41 this.setTaskStatisticsRequirements(this.opts
)
45 public reset (): boolean {
46 this.nextWorkerNodeId
= 0
51 public update (): boolean {
56 public choose (): number {
57 const chosenWorkerNodeKey
= this.nextWorkerNodeId
58 this.nextWorkerNodeId
=
59 this.nextWorkerNodeId
=== this.pool
.workerNodes
.length
- 1
61 : this.nextWorkerNodeId
+ 1
62 return chosenWorkerNodeKey
66 public remove (workerNodeKey
: number): boolean {
67 if (this.nextWorkerNodeId
=== workerNodeKey
) {
68 if (this.pool
.workerNodes
.length
=== 0) {
69 this.nextWorkerNodeId
= 0
70 } else if (this.nextWorkerNodeId
> this.pool
.workerNodes
.length
- 1) {
71 this.nextWorkerNodeId
= this.pool
.workerNodes
.length
- 1