X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fround-robin-worker-choice-strategy.ts;h=1499db945f8297181724db968f21b938d8f3618b;hb=bf90656cacf88d2cfdd5b3262086ba55b2ff9818;hp=a23275873909b0731cb3d514af74f0b9fb59090a;hpb=e65c6cd9a3d6ed2e5b8af95120a5aa070101e945;p=poolifier.git diff --git a/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts b/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts index a2327587..1499db94 100644 --- a/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts @@ -1,5 +1,6 @@ import type { IPoolWorker } from '../pool-worker' import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy' +import type { IWorkerChoiceStrategy } from './selection-strategies-types' /** * Selects the next worker in a round robin fashion. @@ -9,10 +10,12 @@ import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy' * @typeParam Response - Type of response of execution. This can only be serializable data. */ export class RoundRobinWorkerChoiceStrategy< - Worker extends IPoolWorker, - Data, - Response -> extends AbstractWorkerChoiceStrategy { + Worker extends IPoolWorker, + Data, + Response + > + extends AbstractWorkerChoiceStrategy + implements IWorkerChoiceStrategy { /** * Id of the next worker. */ @@ -25,12 +28,23 @@ export class RoundRobinWorkerChoiceStrategy< } /** {@inheritDoc} */ - public choose (): Worker { - const chosenWorker = this.pool.workers[this.nextWorkerId]?.worker + public choose (): number { + const chosenWorkerKey = this.nextWorkerId this.nextWorkerId = this.nextWorkerId === this.pool.workers.length - 1 ? 0 : this.nextWorkerId + 1 - return chosenWorker + return chosenWorkerKey + } + + /** {@inheritDoc} */ + public remove (workerKey: number): boolean { + if (this.nextWorkerId === workerKey) { + this.nextWorkerId = + this.nextWorkerId > this.pool.workers.length - 1 + ? this.pool.workers.length - 1 + : this.nextWorkerId + } + return true } }