})
}
this.workerChoiceStrategyContext.setWorkerChoiceStrategy(
+ this,
workerChoiceStrategy
)
}
Worker extends IPoolWorker,
Data,
Response
-> implements IWorkerChoiceStrategy {
+> implements IWorkerChoiceStrategy<Worker, Data, Response> {
/** {@inheritDoc} */
public readonly isDynamicPool: boolean
/** {@inheritDoc} */
* @param pool - The pool instance.
*/
public constructor (
- protected readonly pool: IPoolInternal<Worker, Data, Response>
+ public readonly pool: IPoolInternal<Worker, Data, Response>
) {
this.isDynamicPool = this.pool.type === PoolType.DYNAMIC
this.choose.bind(this)
Response
>
extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
- implements IWorkerChoiceStrategy {
+ implements IWorkerChoiceStrategy<Worker, Data, Response> {
/** {@inheritDoc} */
public readonly requiredStatistics: RequiredStatistics = {
runTime: true,
Response
>
extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
- implements IWorkerChoiceStrategy {
+ implements IWorkerChoiceStrategy<Worker, Data, Response> {
/** {@inheritDoc} */
public readonly requiredStatistics: RequiredStatistics = {
runTime: true,
Response
>
extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
- implements IWorkerChoiceStrategy {
+ implements IWorkerChoiceStrategy<Worker, Data, Response> {
/** {@inheritDoc} */
public reset (): boolean {
return true
Response
>
extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
- implements IWorkerChoiceStrategy {
+ implements IWorkerChoiceStrategy<Worker, Data, Response> {
/**
* Id of the next worker.
*/
+import type { IPoolInternal } from '../pool-internal'
+import type { IPoolWorker } from '../pool-worker'
+
/**
* Enumeration of worker choice strategies.
*/
/**
* Worker choice strategy interface.
*/
-export interface IWorkerChoiceStrategy {
+export interface IWorkerChoiceStrategy<
+ Worker extends IPoolWorker,
+ Data = unknown,
+ Response = unknown
+> {
+ /**
+ * The pool instance.
+ * @readonly
+ */
+ readonly pool: IPoolInternal<Worker, Data, Response>
/**
* Is the pool attached to the strategy dynamic?.
+ * @readonly
*/
readonly isDynamicPool: boolean
/**
* Required pool tasks usage statistics.
+ * @readonly
*/
readonly requiredStatistics: RequiredStatistics
/**
> (
pool: IPoolInternal<Worker, Data, Response>,
workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
-): IWorkerChoiceStrategy {
+): IWorkerChoiceStrategy<Worker, Data, Response> {
switch (workerChoiceStrategy) {
case WorkerChoiceStrategies.ROUND_ROBIN:
return new RoundRobinWorkerChoiceStrategy<Worker, Data, Response>(pool)
Response
>
extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
- implements IWorkerChoiceStrategy {
+ implements IWorkerChoiceStrategy<Worker, Data, Response> {
/** {@inheritDoc} */
public readonly requiredStatistics: RequiredStatistics = {
runTime: true,
Data,
Response
> {
- private workerChoiceStrategy!: IWorkerChoiceStrategy
+ private workerChoiceStrategy: IWorkerChoiceStrategy<Worker, Data, Response>
/**
* Worker choice strategy context constructor.
* @param workerChoiceStrategy - The worker choice strategy.
*/
public constructor (
- private readonly pool: IPoolInternal<Worker, Data, Response>,
+ pool: IPoolInternal<Worker, Data, Response>,
private readonly createWorkerCallback: () => number,
workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
) {
this.execute.bind(this)
- this.setWorkerChoiceStrategy(workerChoiceStrategy)
+ this.workerChoiceStrategy = getWorkerChoiceStrategy<Worker, Data, Response>(
+ pool,
+ workerChoiceStrategy
+ )
}
/**
* @param workerChoiceStrategy - The worker choice strategy to set.
*/
public setWorkerChoiceStrategy (
+ pool: IPoolInternal<Worker, Data, Response>,
workerChoiceStrategy: WorkerChoiceStrategy
): void {
this.workerChoiceStrategy?.reset()
this.workerChoiceStrategy = getWorkerChoiceStrategy<Worker, Data, Response>(
- this.pool,
+ pool,
workerChoiceStrategy
)
}
public execute (): number {
if (
this.workerChoiceStrategy.isDynamicPool &&
- !this.pool.full &&
- this.pool.findFreeWorkerKey() === -1
+ !this.workerChoiceStrategy.pool.full &&
+ this.workerChoiceStrategy.pool.findFreeWorkerKey() === -1
) {
return this.createWorkerCallback()
}
fixedPool
)
workerChoiceStrategyContext.setWorkerChoiceStrategy(
+ fixedPool,
WorkerChoiceStrategies.ROUND_ROBIN
)
expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
dynamicPool
)
workerChoiceStrategyContext.setWorkerChoiceStrategy(
+ dynamicPool,
WorkerChoiceStrategies.ROUND_ROBIN
)
expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
fixedPool
)
workerChoiceStrategyContext.setWorkerChoiceStrategy(
+ fixedPool,
WorkerChoiceStrategies.LESS_USED
)
expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
dynamicPool
)
workerChoiceStrategyContext.setWorkerChoiceStrategy(
+ dynamicPool,
WorkerChoiceStrategies.LESS_USED
)
expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
fixedPool
)
workerChoiceStrategyContext.setWorkerChoiceStrategy(
+ fixedPool,
WorkerChoiceStrategies.LESS_BUSY
)
expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
dynamicPool
)
workerChoiceStrategyContext.setWorkerChoiceStrategy(
+ dynamicPool,
WorkerChoiceStrategies.LESS_BUSY
)
expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
fixedPool
)
workerChoiceStrategyContext.setWorkerChoiceStrategy(
+ fixedPool,
WorkerChoiceStrategies.FAIR_SHARE
)
expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
dynamicPool
)
workerChoiceStrategyContext.setWorkerChoiceStrategy(
+ dynamicPool,
WorkerChoiceStrategies.FAIR_SHARE
)
expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
fixedPool
)
workerChoiceStrategyContext.setWorkerChoiceStrategy(
+ fixedPool,
WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
)
expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
dynamicPool
)
workerChoiceStrategyContext.setWorkerChoiceStrategy(
+ dynamicPool,
WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
)
expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(