X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fround-robin-worker-choice-strategy.ts;h=7a50db7b6387d5426dd4c4847482c5314f611b74;hb=f6bc9f26d8a0246bbee14b2b03d0bcc41b8aeb52;hp=0995b3506b102931ab59956a927a51f0aa5fbbbc;hpb=d33be4309c69e39da5e81479e40b1a5ec7078bd5;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 0995b350..7a50db7b 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' @@ -12,8 +11,8 @@ import type { * Selects the next worker in a round robin fashion. * * @typeParam Worker - Type of worker which manages the strategy. - * @typeParam Data - Type of data sent to the worker. This can only be serializable data. - * @typeParam Response - Type of execution response. This can only be serializable data. + * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data. + * @typeParam Response - Type of execution response. This can only be structured-cloneable data. */ export class RoundRobinWorkerChoiceStrategy< Worker extends IWorker, @@ -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.nextWorkerNodeId = 0 + this.nextWorkerNodeKey = 0 return true } @@ -48,24 +42,34 @@ export class RoundRobinWorkerChoiceStrategy< } /** @inheritDoc */ - public choose (): number { - const chosenWorkerNodeKey = this.nextWorkerNodeId - this.nextWorkerNodeId = - this.nextWorkerNodeId === this.pool.workerNodes.length - 1 - ? 0 - : this.nextWorkerNodeId + 1 + public choose (): number | undefined { + const chosenWorkerNodeKey = this.nextWorkerNodeKey + this.roundRobinNextWorkerNodeKey() + if (!this.isWorkerNodeEligible(this.nextWorkerNodeKey as number)) { + this.nextWorkerNodeKey = undefined + this.previousWorkerNodeKey = + chosenWorkerNodeKey ?? this.previousWorkerNodeKey + } return chosenWorkerNodeKey } /** @inheritDoc */ public remove (workerNodeKey: number): boolean { - if (this.nextWorkerNodeId === workerNodeKey) { + if (this.nextWorkerNodeKey === workerNodeKey) { if (this.pool.workerNodes.length === 0) { - this.nextWorkerNodeId = 0 - } else if (this.nextWorkerNodeId > this.pool.workerNodes.length - 1) { - this.nextWorkerNodeId = this.pool.workerNodes.length - 1 + this.nextWorkerNodeKey = 0 + } else if (this.nextWorkerNodeKey > this.pool.workerNodes.length - 1) { + this.nextWorkerNodeKey = this.pool.workerNodes.length - 1 } } return true } + + private roundRobinNextWorkerNodeKey (): number | undefined { + this.nextWorkerNodeKey = + this.nextWorkerNodeKey === this.pool.workerNodes.length - 1 + ? 0 + : (this.nextWorkerNodeKey ?? this.previousWorkerNodeKey) + 1 + return this.nextWorkerNodeKey + } }