X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fround-robin-worker-choice-strategy.ts;h=41bc4085ebd5a1399975e48e873fc0612c6a611c;hb=94407def0f4cd4356982ce5144a6108aec9a1ff9;hp=936925313f99e9bafa6c3bb8c0a71f7ad0c94fad;hpb=6c6afb8463782af0689101b7c67cea80df83018f;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 93692531..41bc4085 100644 --- a/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts @@ -12,8 +12,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, @@ -24,14 +24,10 @@ export class RoundRobinWorkerChoiceStrategy< implements IWorkerChoiceStrategy { /** @inheritDoc */ public readonly strategyPolicy: StrategyPolicy = { - useDynamicWorker: true + dynamicWorkerUsage: false, + dynamicWorkerReady: true } - /** - * Id of the next worker node. - */ - private nextWorkerNodeId: number = 0 - /** @inheritDoc */ public constructor ( pool: IPool, @@ -43,7 +39,7 @@ export class RoundRobinWorkerChoiceStrategy< /** @inheritDoc */ public reset (): boolean { - this.nextWorkerNodeId = 0 + this.nextWorkerNodeKey = 0 return true } @@ -53,24 +49,32 @@ 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 + } 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 ?? 0) + 1 + return this.nextWorkerNodeKey + } }