X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fweighted-round-robin-worker-choice-strategy.ts;h=96b5867b5bfb1db98f8a8afd4505b5c24da75505;hb=78099a150dc54d7adab495195fa5f133fd54e114;hp=3709cce409b4ba988a728f81f0f8c6eb65f205f8;hpb=c6bd2650c2690bd84951a2278820adde1b05b41b;p=poolifier.git diff --git a/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts b/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts index 3709cce4..96b5867b 100644 --- a/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts @@ -25,15 +25,16 @@ interface TaskRunTime { */ export class WeightedRoundRobinWorkerChoiceStrategy< Worker extends IPoolWorker, - Data, - Response + Data = unknown, + Response = unknown > extends AbstractWorkerChoiceStrategy implements IWorkerChoiceStrategy { - /** {@inheritDoc} */ + /** @inheritDoc */ public readonly requiredStatistics: RequiredStatistics = { runTime: true, - avgRunTime: true + avgRunTime: true, + medRunTime: false } /** @@ -63,7 +64,7 @@ export class WeightedRoundRobinWorkerChoiceStrategy< this.initWorkersTaskRunTime() } - /** {@inheritDoc} */ + /** @inheritDoc */ public reset (): boolean { this.currentWorkerId = 0 this.workersTaskRunTime.clear() @@ -71,7 +72,7 @@ export class WeightedRoundRobinWorkerChoiceStrategy< return true } - /** {@inheritDoc} */ + /** @inheritDoc */ public choose (): number { const chosenWorkerKey = this.currentWorkerId if (this.isDynamicPool && !this.workersTaskRunTime.has(chosenWorkerKey)) { @@ -99,13 +100,17 @@ export class WeightedRoundRobinWorkerChoiceStrategy< return chosenWorkerKey } - /** {@inheritDoc} */ + /** @inheritDoc */ public remove (workerKey: number): boolean { if (this.currentWorkerId === workerKey) { - this.currentWorkerId = - this.currentWorkerId > this.pool.workers.length - 1 - ? this.pool.workers.length - 1 - : this.currentWorkerId + if (this.pool.workers.length === 0) { + this.currentWorkerId = 0 + } else { + this.currentWorkerId = + this.currentWorkerId > this.pool.workers.length - 1 + ? this.pool.workers.length - 1 + : this.currentWorkerId + } } const workerDeleted = this.workersTaskRunTime.delete(workerKey) for (const [key, value] of this.workersTaskRunTime) {