import { DynamicPoolWorkerChoiceStrategy } from './dynamic-pool-worker-choice-strategy'
import type {
IWorkerChoiceStrategy,
+ RequiredStatistics,
WorkerChoiceStrategy
} from './selection-strategies-types'
import { WorkerChoiceStrategies } from './selection-strategies-types'
Data,
Response
> {
- private workerChoiceStrategy!: IWorkerChoiceStrategy<Worker>
+ 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<Worker, Data, Response>,
- private readonly createDynamicallyWorkerCallback: () => Worker,
+ private readonly createWorkerCallback: () => number,
workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
) {
this.setWorkerChoiceStrategy(workerChoiceStrategy)
*/
private getPoolWorkerChoiceStrategy (
workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
- ): IWorkerChoiceStrategy<Worker> {
+ ): IWorkerChoiceStrategy {
if (this.pool.type === PoolType.DYNAMIC) {
return new DynamicPoolWorkerChoiceStrategy(
this.pool,
- this.createDynamicallyWorkerCallback,
+ this.createWorkerCallback,
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<Worker> {
- return this.workerChoiceStrategy
+ public getRequiredStatistics (): RequiredStatistics {
+ return this.workerChoiceStrategy.requiredStatistics
}
/**
/**
* 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)
+ }
}