1 import type { IPoolWorker
} from
'../pool-worker'
2 import { AbstractWorkerChoiceStrategy
} from
'./abstract-worker-choice-strategy'
3 import type { IWorkerChoiceStrategy
} from
'./selection-strategies-types'
6 * Selects the next worker in a round robin fashion.
8 * @typeParam Worker - Type of worker which manages the strategy.
9 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
10 * @typeParam Response - Type of response of execution. This can only be serializable data.
12 export class RoundRobinWorkerChoiceStrategy
<
13 Worker
extends IPoolWorker
,
17 extends AbstractWorkerChoiceStrategy
<Worker
, Data
, Response
>
18 implements IWorkerChoiceStrategy
{
20 * Id of the next worker.
22 private nextWorkerId
: number = 0
25 public reset (): boolean {
31 public choose (): number {
32 const chosenWorkerKey
= this.nextWorkerId
34 this.nextWorkerId
=== this.pool
.workers
.length
- 1
36 : this.nextWorkerId
+ 1
37 return chosenWorkerKey
41 public remove (workerKey
: number): boolean {
42 if (this.nextWorkerId
=== workerKey
) {
44 this.nextWorkerId
> this.pool
.workers
.length
- 1
45 ? this.pool
.workers
.length
- 1