X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fround-robin-worker-choice-strategy.ts;h=aa3ee108ced7d528f1d5ea90204c4306f841226c;hb=facb41d7d33bd6c11970da5e946c51347824fd54;hp=55fa8ad73d4c7b7713a490cf07265e3634803779;hpb=8990357d855c45cd0063f24092bb58b4163ddb0a;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 55fa8ad7..aa3ee108 100644 --- a/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts @@ -4,7 +4,6 @@ import type { IWorker } from '../worker' import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy' import type { IWorkerChoiceStrategy, - StrategyPolicy, WorkerChoiceStrategyOptions } from './selection-strategies-types' @@ -22,11 +21,6 @@ export class RoundRobinWorkerChoiceStrategy< > extends AbstractWorkerChoiceStrategy implements IWorkerChoiceStrategy { - /** @inheritDoc */ - public readonly strategyPolicy: StrategyPolicy = { - useDynamicWorker: true - } - /** @inheritDoc */ public constructor ( pool: IPool, @@ -38,7 +32,7 @@ export class RoundRobinWorkerChoiceStrategy< /** @inheritDoc */ public reset (): boolean { - this.nextWorkerNodeKey = 0 + this.resetWorkerNodeKeyProperties() return true } @@ -48,31 +42,38 @@ export class RoundRobinWorkerChoiceStrategy< } /** @inheritDoc */ - public choose (): number { + public choose (): number | undefined { const chosenWorkerNodeKey = this.nextWorkerNodeKey - do { - this.roundRobinNextWorkerNodeKey() - } while (!this.isWorkerNodeEligible(this.nextWorkerNodeKey)) + this.setPreviousWorkerNodeKey(chosenWorkerNodeKey) + this.roundRobinNextWorkerNodeKey() return chosenWorkerNodeKey } /** @inheritDoc */ public remove (workerNodeKey: number): boolean { - if (this.nextWorkerNodeKey === workerNodeKey) { - if (this.pool.workerNodes.length === 0) { - this.nextWorkerNodeKey = 0 - } else if (this.nextWorkerNodeKey > this.pool.workerNodes.length - 1) { - this.nextWorkerNodeKey = this.pool.workerNodes.length - 1 - } + if (this.pool.workerNodes.length === 0) { + this.reset() + } + if ( + this.nextWorkerNodeKey === workerNodeKey && + this.nextWorkerNodeKey > this.pool.workerNodes.length - 1 + ) { + this.nextWorkerNodeKey = this.pool.workerNodes.length - 1 + } + if ( + this.previousWorkerNodeKey === workerNodeKey && + this.previousWorkerNodeKey > this.pool.workerNodes.length - 1 + ) { + this.previousWorkerNodeKey = this.pool.workerNodes.length - 1 } return true } - private roundRobinNextWorkerNodeKey (): number { + private roundRobinNextWorkerNodeKey (): number | undefined { this.nextWorkerNodeKey = this.nextWorkerNodeKey === this.pool.workerNodes.length - 1 ? 0 - : this.nextWorkerNodeKey + 1 + : (this.nextWorkerNodeKey ?? this.previousWorkerNodeKey) + 1 return this.nextWorkerNodeKey } }