X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fworker-choice-strategy-context.ts;h=bd616a54d666ffc3372d09e040686e3e9aa1aca2;hb=5502c07c81463257e1dd3015ee5a828846ece0b2;hp=1bba1a55eccdc82a02d6c0849c2413c68b70b9dc;hpb=38e795c12f0e9daeff7b025147f36f85f486366e;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 1bba1a55..bd616a54 100644 --- a/src/pools/selection-strategies/worker-choice-strategy-context.ts +++ b/src/pools/selection-strategies/worker-choice-strategy-context.ts @@ -4,6 +4,7 @@ import type { IPoolWorker } from '../pool-worker' import { DynamicPoolWorkerChoiceStrategy } from './dynamic-pool-worker-choice-strategy' import type { IWorkerChoiceStrategy, + RequiredStatistics, WorkerChoiceStrategy } from './selection-strategies-types' import { WorkerChoiceStrategies } from './selection-strategies-types' @@ -21,18 +22,18 @@ export class WorkerChoiceStrategyContext< Data, Response > { - private workerChoiceStrategy!: IWorkerChoiceStrategy + private workerChoiceStrategy!: IWorkerChoiceStrategy /** * Worker choice strategy context constructor. * * @param pool - The pool instance. - * @param createDynamicallyWorkerCallback - The worker creation callback for dynamic pool. + * @param createWorkerCallback - The worker creation callback for dynamic pool. * @param workerChoiceStrategy - The worker choice strategy. */ public constructor ( private readonly pool: IPoolInternal, - private readonly createDynamicallyWorkerCallback: () => Worker, + private readonly createWorkerCallback: () => number, workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN ) { this.setWorkerChoiceStrategy(workerChoiceStrategy) @@ -46,11 +47,11 @@ export class WorkerChoiceStrategyContext< */ private getPoolWorkerChoiceStrategy ( workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN - ): IWorkerChoiceStrategy { + ): IWorkerChoiceStrategy { if (this.pool.type === PoolType.DYNAMIC) { return new DynamicPoolWorkerChoiceStrategy( this.pool, - this.createDynamicallyWorkerCallback, + this.createWorkerCallback, workerChoiceStrategy ) } @@ -58,12 +59,12 @@ export class WorkerChoiceStrategyContext< } /** - * Gets the worker choice strategy used in the context. + * Gets the worker choice strategy required statistics. * - * @returns The worker choice strategy. + * @returns The required statistics. */ - public getWorkerChoiceStrategy (): IWorkerChoiceStrategy { - return this.workerChoiceStrategy + public getRequiredStatistics (): RequiredStatistics { + return this.workerChoiceStrategy.requiredStatistics } /** @@ -82,9 +83,19 @@ export class WorkerChoiceStrategyContext< /** * Chooses a worker with the underlying selection strategy. * - * @returns The chosen one. + * @returns The key of the chosen one. */ - public execute (): Worker { + public execute (): number { return this.workerChoiceStrategy.choose() } + + /** + * Removes a worker in the underlying selection strategy internals. + * + * @param workerKey - The key of the worker to remove. + * @returns `true` if the removal is successful, `false` otherwise. + */ + public remove (workerKey: number): boolean { + return this.workerChoiceStrategy.remove(workerKey) + } }