Fix strategies internals reset
[poolifier.git] / src / pools / selection-strategies / round-robin-worker-choice-strategy.ts
CommitLineData
ea7a90d3 1import type { IPoolWorker } from '../pool-worker'
bdaf31cd
JB
2import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
3
4/**
5 * Selects the next worker in a round robin fashion.
6 *
7 * @template Worker Type of worker which manages the strategy.
8 * @template Data Type of data sent to the worker. This can only be serializable data.
9 * @template Response Type of response of execution. This can only be serializable data.
10 */
11export class RoundRobinWorkerChoiceStrategy<
ea7a90d3 12 Worker extends IPoolWorker,
bdaf31cd
JB
13 Data,
14 Response
15> extends AbstractWorkerChoiceStrategy<Worker, Data, Response> {
16 /**
17 * Index for the next worker.
18 */
19 private nextWorkerIndex: number = 0
20
ea7a90d3 21 /** @inheritDoc */
a6f7f1b4
JB
22 public reset (): boolean {
23 this.nextWorkerIndex = 0
ea7a90d3
JB
24 return true
25 }
26
a76fac14 27 /** @inheritDoc */
bdaf31cd
JB
28 public choose (): Worker {
29 const chosenWorker = this.pool.workers[this.nextWorkerIndex]
30 this.nextWorkerIndex =
31 this.nextWorkerIndex === this.pool.workers.length - 1
32 ? 0
33 : this.nextWorkerIndex + 1
34 return chosenWorker
35 }
36}