X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fworker-choice-strategy-context.ts;h=41b515b816d00dc991c27d8143efe80ec7c6116a;hb=3300e7bcb155358c2cc1eed6e4ac11581457616f;hp=836a1cb5052c5f2219e9e872b3a979684b0336af;hpb=6ffb8e34e2276cc0e0f5576749e190d1d914b7d4;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 836a1cb5..41b515b8 100644 --- a/src/pools/selection-strategies/worker-choice-strategy-context.ts +++ b/src/pools/selection-strategies/worker-choice-strategy-context.ts @@ -1,94 +1,94 @@ import type { IPoolInternal } from '../pool-internal' -import { PoolType } from '../pool-internal' 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' -import { SelectionStrategiesUtils } from './selection-strategies-utils' +import { getWorkerChoiceStrategy } from './selection-strategies-utils' /** * The worker choice strategy context. * - * @template Worker Type of worker. - * @template Data Type of data sent to the worker. This can only be serializable data. - * @template Response Type of response of execution. This can only be serializable data. + * @typeParam Worker - Type of worker. + * @typeParam Data - Type of data sent to the worker. This can only be serializable data. + * @typeParam Response - Type of response of execution. This can only be serializable data. */ export class WorkerChoiceStrategyContext< Worker extends IPoolWorker, 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 workerChoiceStrategy The worker choice strategy. + * @param pool - The pool instance. + * @param createWorkerCallback - The worker creation callback for dynamic pool. + * @param workerChoiceStrategy - The worker choice strategy. */ public constructor ( - private readonly pool: IPoolInternal, - private createDynamicallyWorkerCallback: () => Worker, + pool: IPoolInternal, + private readonly createWorkerCallback: () => number, workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN ) { - this.setWorkerChoiceStrategy(workerChoiceStrategy) - } - - /** - * Gets the worker choice strategy instance specific to the pool type. - * - * @param workerChoiceStrategy The worker choice strategy. - * @returns The worker choice strategy instance for the pool type. - */ - private getPoolWorkerChoiceStrategy ( - workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN - ): IWorkerChoiceStrategy { - if (this.pool.type === PoolType.DYNAMIC) { - return new DynamicPoolWorkerChoiceStrategy( - this.pool, - this.createDynamicallyWorkerCallback, - workerChoiceStrategy - ) - } - return SelectionStrategiesUtils.getWorkerChoiceStrategy( - this.pool, + this.execute.bind(this) + this.workerChoiceStrategy = getWorkerChoiceStrategy( + pool, workerChoiceStrategy ) } /** - * 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 } /** * Sets the worker choice strategy to use in the context. * - * @param workerChoiceStrategy The worker choice strategy to set. + * @param workerChoiceStrategy - The worker choice strategy to set. */ public setWorkerChoiceStrategy ( + pool: IPoolInternal, workerChoiceStrategy: WorkerChoiceStrategy ): void { this.workerChoiceStrategy?.reset() - this.workerChoiceStrategy = this.getPoolWorkerChoiceStrategy( + this.workerChoiceStrategy = getWorkerChoiceStrategy( + pool, workerChoiceStrategy ) } /** - * Chooses a worker with the underlying selection strategy. + * Chooses a worker with the worker choice strategy. * - * @returns The chosen one. + * @returns The key of the chosen one. */ - public execute (): Worker { + public execute (): number { + if ( + this.workerChoiceStrategy.isDynamicPool && + !this.workerChoiceStrategy.pool.full && + this.workerChoiceStrategy.pool.findFreeWorkerKey() === -1 + ) { + return this.createWorkerCallback() + } return this.workerChoiceStrategy.choose() } + + /** + * Removes a worker in the worker choice 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) + } }