X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fworker-choice-strategy-context.ts;h=e5fddc1b1fc5b400398b2e86bd0ac3b3fd6f97c9;hb=a4958de2101f06e7096b83adbca82fcfd532a721;hp=b075c4eac5ae3cd9cd88b41553263b11e2e5af95;hpb=d710242dd39f5dd418b0a89536a9ad88c147fe3b;p=poolifier.git diff --git a/src/pools/selection-strategies/worker-choice-strategy-context.ts b/src/pools/selection-strategies/worker-choice-strategy-context.ts index b075c4ea..e5fddc1b 100644 --- a/src/pools/selection-strategies/worker-choice-strategy-context.ts +++ b/src/pools/selection-strategies/worker-choice-strategy-context.ts @@ -43,33 +43,46 @@ export class WorkerChoiceStrategyContext< private workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN, opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS ) { - this.execute.bind(this) + this.execute = this.execute.bind(this) this.workerChoiceStrategies = new Map< WorkerChoiceStrategy, IWorkerChoiceStrategy >([ [ WorkerChoiceStrategies.ROUND_ROBIN, - new RoundRobinWorkerChoiceStrategy(pool, opts) + new (RoundRobinWorkerChoiceStrategy.bind(this))( + pool, + opts + ) ], [ WorkerChoiceStrategies.LESS_USED, - new LessUsedWorkerChoiceStrategy(pool, opts) + new (LessUsedWorkerChoiceStrategy.bind(this))( + pool, + opts + ) ], [ WorkerChoiceStrategies.LESS_BUSY, - new LessBusyWorkerChoiceStrategy(pool, opts) + new (LessBusyWorkerChoiceStrategy.bind(this))( + pool, + opts + ) ], [ WorkerChoiceStrategies.FAIR_SHARE, - new FairShareWorkerChoiceStrategy(pool, opts) - ], - [ - WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN, - new WeightedRoundRobinWorkerChoiceStrategy( + new (FairShareWorkerChoiceStrategy.bind(this))( pool, opts ) + ], + [ + WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN, + new (WeightedRoundRobinWorkerChoiceStrategy.bind(this))< + Worker, + Data, + Response + >(pool, opts) ] ]) } @@ -101,6 +114,19 @@ export class WorkerChoiceStrategyContext< this.workerChoiceStrategies.get(this.workerChoiceStrategy)?.reset() } + /** + * Updates the worker choice strategy internals in the context. + * + * @returns `true` if the update is successful, `false` otherwise. + */ + public update (workerNodeKey: number): boolean { + return ( + this.workerChoiceStrategies.get( + this.workerChoiceStrategy + ) as IWorkerChoiceStrategy + ).update(workerNodeKey) + } + /** * Executes the worker choice strategy algorithm in the context. * @@ -127,4 +153,15 @@ export class WorkerChoiceStrategyContext< ) as IWorkerChoiceStrategy ).remove(workerNodeKey) } + + /** + * Sets the worker choice strategies in the context options. + * + * @param opts - The worker choice strategy options. + */ + public setOptions (opts: WorkerChoiceStrategyOptions): void { + this.workerChoiceStrategies.forEach(workerChoiceStrategy => { + workerChoiceStrategy.setOptions(opts) + }) + } }