X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fround-robin-worker-choice-strategy.ts;h=55fa8ad73d4c7b7713a490cf07265e3634803779;hb=962b9863b80f507c6ce307f9b65b69fe2feb6081;hp=107e9639b38466e3d832fd217885b21b27206f12;hpb=2fc5cae38554901a21435ef087036a062c9d1632;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 107e9639..55fa8ad7 100644 --- a/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts @@ -4,6 +4,7 @@ import type { IWorker } from '../worker' import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy' import type { IWorkerChoiceStrategy, + StrategyPolicy, WorkerChoiceStrategyOptions } from './selection-strategies-types' @@ -11,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, @@ -21,10 +22,10 @@ export class RoundRobinWorkerChoiceStrategy< > extends AbstractWorkerChoiceStrategy implements IWorkerChoiceStrategy { - /** - * Id of the next worker node. - */ - private nextWorkerNodeId: number = 0 + /** @inheritDoc */ + public readonly strategyPolicy: StrategyPolicy = { + useDynamicWorker: true + } /** @inheritDoc */ public constructor ( @@ -32,37 +33,46 @@ export class RoundRobinWorkerChoiceStrategy< opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS ) { super(pool, opts) - this.checkOptions(opts) + this.setTaskStatisticsRequirements(this.opts) } /** @inheritDoc */ public reset (): boolean { - this.nextWorkerNodeId = 0 + this.nextWorkerNodeKey = 0 + return true + } + + /** @inheritDoc */ + public update (): boolean { return true } /** @inheritDoc */ public choose (): number { - const chosenWorkerNodeKey = this.nextWorkerNodeId - this.nextWorkerNodeId = - this.nextWorkerNodeId === this.pool.workerNodes.length - 1 - ? 0 - : this.nextWorkerNodeId + 1 + const chosenWorkerNodeKey = this.nextWorkerNodeKey + do { + this.roundRobinNextWorkerNodeKey() + } while (!this.isWorkerNodeEligible(this.nextWorkerNodeKey)) 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 { - this.nextWorkerNodeId = - this.nextWorkerNodeId > this.pool.workerNodes.length - 1 - ? this.pool.workerNodes.length - 1 - : this.nextWorkerNodeId + this.nextWorkerNodeKey = 0 + } else if (this.nextWorkerNodeKey > this.pool.workerNodes.length - 1) { + this.nextWorkerNodeKey = this.pool.workerNodes.length - 1 } } return true } + + private roundRobinNextWorkerNodeKey (): number { + this.nextWorkerNodeKey = + this.nextWorkerNodeKey === this.pool.workerNodes.length - 1 + ? 0 + : this.nextWorkerNodeKey + 1 + return this.nextWorkerNodeKey + } }