X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Finterleaved-weighted-round-robin-worker-choice-strategy.ts;h=26f9d909eed84f448e0244042d23dc289a26b96a;hb=43b84974f72aecd75b4b592e0009359bd5baf228;hp=85f3148374e5b0c5cf932425772779839f217ca0;hpb=d33be4309c69e39da5e81479e40b1a5ec7078bd5;p=poolifier.git diff --git a/src/pools/selection-strategies/interleaved-weighted-round-robin-worker-choice-strategy.ts b/src/pools/selection-strategies/interleaved-weighted-round-robin-worker-choice-strategy.ts index 85f31483..26f9d909 100644 --- a/src/pools/selection-strategies/interleaved-weighted-round-robin-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/interleaved-weighted-round-robin-worker-choice-strategy.ts @@ -12,8 +12,8 @@ import type { * Selects the next worker with an interleaved weighted round robin scheduling algorithm. * * @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 InterleavedWeightedRoundRobinWorkerChoiceStrategy< Worker extends IWorker, @@ -54,7 +54,7 @@ export class InterleavedWeightedRoundRobinWorkerChoiceStrategy< /** @inheritDoc */ public reset (): boolean { - this.nextWorkerNodeId = 0 + this.nextWorkerNodeKey = 0 this.roundId = 0 return true } @@ -74,13 +74,16 @@ export class InterleavedWeightedRoundRobinWorkerChoiceStrategy< roundIndex++ ) { for ( - let workerNodeKey = this.nextWorkerNodeId; + let workerNodeKey = this.nextWorkerNodeKey; workerNodeKey < this.pool.workerNodes.length; workerNodeKey++ ) { const workerWeight = this.opts.weights?.[workerNodeKey] ?? this.defaultWorkerWeight - if (workerWeight >= this.roundWeights[roundIndex]) { + if ( + this.isWorkerNodeReady(workerNodeKey) && + workerWeight >= this.roundWeights[roundIndex] + ) { roundId = roundIndex workerNodeId = workerNodeKey break @@ -88,25 +91,25 @@ export class InterleavedWeightedRoundRobinWorkerChoiceStrategy< } } this.roundId = roundId ?? 0 - this.nextWorkerNodeId = workerNodeId ?? 0 - const chosenWorkerNodeKey = this.nextWorkerNodeId - if (this.nextWorkerNodeId === this.pool.workerNodes.length - 1) { - this.nextWorkerNodeId = 0 + this.nextWorkerNodeKey = workerNodeId ?? 0 + const chosenWorkerNodeKey = this.nextWorkerNodeKey + if (this.nextWorkerNodeKey === this.pool.workerNodes.length - 1) { + this.nextWorkerNodeKey = 0 this.roundId = this.roundId === this.roundWeights.length - 1 ? 0 : this.roundId + 1 } else { - this.nextWorkerNodeId = this.nextWorkerNodeId + 1 + this.nextWorkerNodeKey = this.nextWorkerNodeKey + 1 } 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 this.roundId = this.roundId === this.roundWeights.length - 1 ? 0 : this.roundId + 1 }