private checkPoolOptions (opts: PoolOptions<Worker>): void {
this.opts.workerChoiceStrategy =
opts.workerChoiceStrategy ?? WorkerChoiceStrategies.ROUND_ROBIN
+ if (
+ !Object.values(WorkerChoiceStrategies).includes(
+ this.opts.workerChoiceStrategy
+ )
+ ) {
+ throw new Error(
+ `Invalid worker choice strategy '${this.opts.workerChoiceStrategy}'`
+ )
+ }
this.opts.enableEvents = opts.enableEvents ?? true
}
Data = unknown,
Response = unknown
> {
- private readonly workerChoiceStrategies = new Map<
+ private readonly workerChoiceStrategies: Map<
WorkerChoiceStrategy,
IWorkerChoiceStrategy<Worker, Data, Response>
- >()
+ >
/**
* Worker choice strategy context constructor.
private workerChoiceStrategyType: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
) {
this.execute.bind(this)
- this.registerWorkerChoiceStrategy(pool, workerChoiceStrategyType)
+ this.workerChoiceStrategies = new Map<
+ WorkerChoiceStrategy,
+ IWorkerChoiceStrategy<Worker, Data, Response>
+ >([
+ [
+ WorkerChoiceStrategies.ROUND_ROBIN,
+ new RoundRobinWorkerChoiceStrategy<Worker, Data, Response>(pool)
+ ],
+ [
+ WorkerChoiceStrategies.LESS_USED,
+ new LessUsedWorkerChoiceStrategy<Worker, Data, Response>(pool)
+ ],
+ [
+ WorkerChoiceStrategies.LESS_BUSY,
+ new LessBusyWorkerChoiceStrategy<Worker, Data, Response>(pool)
+ ],
+ [
+ WorkerChoiceStrategies.FAIR_SHARE,
+ new FairShareWorkerChoiceStrategy<Worker, Data, Response>(pool)
+ ],
+ [
+ WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN,
+ new WeightedRoundRobinWorkerChoiceStrategy<Worker, Data, Response>(pool)
+ ]
+ ])
}
/**
this.workerChoiceStrategies.get(workerChoiceStrategy)?.reset()
} else {
this.workerChoiceStrategyType = workerChoiceStrategy
- this.registerWorkerChoiceStrategy(pool, workerChoiceStrategy)
}
}
) as IWorkerChoiceStrategy<Worker, Data, Response>
).remove(workerKey)
}
-
- private registerWorkerChoiceStrategy (
- pool: IPoolInternal<Worker, Data, Response>,
- workerChoiceStrategy: WorkerChoiceStrategy
- ): void {
- if (!this.workerChoiceStrategies.has(workerChoiceStrategy)) {
- this.workerChoiceStrategies.set(
- workerChoiceStrategy,
- this.getWorkerChoiceStrategy(pool, workerChoiceStrategy)
- )
- }
- }
-
- /**
- * Gets the worker choice strategy instance.
- *
- * @param pool - The pool instance.
- * @param workerChoiceStrategy - The worker choice strategy.
- * @returns The worker choice strategy instance.
- */
- private getWorkerChoiceStrategy (
- pool: IPoolInternal<Worker, Data, Response>,
- workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
- ): IWorkerChoiceStrategy<Worker, Data, Response> {
- switch (workerChoiceStrategy) {
- case WorkerChoiceStrategies.ROUND_ROBIN:
- return new RoundRobinWorkerChoiceStrategy<Worker, Data, Response>(pool)
- case WorkerChoiceStrategies.LESS_USED:
- return new LessUsedWorkerChoiceStrategy<Worker, Data, Response>(pool)
- case WorkerChoiceStrategies.LESS_BUSY:
- return new LessBusyWorkerChoiceStrategy<Worker, Data, Response>(pool)
- case WorkerChoiceStrategies.FAIR_SHARE:
- return new FairShareWorkerChoiceStrategy<Worker, Data, Response>(pool)
- case WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN:
- return new WeightedRoundRobinWorkerChoiceStrategy<
- Worker,
- Data,
- Response
- >(pool)
- default:
- throw new Error(
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
- `Worker choice strategy '${workerChoiceStrategy}' not found`
- )
- }
- }
}
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
WorkerChoiceStrategies.ROUND_ROBIN
- )?.nextWorkerId
- ).toBeUndefined()
+ ).nextWorkerId
+ ).toBeDefined()
pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN)
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
WorkerChoiceStrategies.ROUND_ROBIN
- )?.nextWorkerId
- ).toBeUndefined()
+ ).nextWorkerId
+ ).toBeDefined()
pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN)
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
WorkerChoiceStrategies.FAIR_SHARE
- )?.workerLastVirtualTaskTimestamp
- ).toBeUndefined()
+ ).workerLastVirtualTaskTimestamp
+ ).toBeDefined()
pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
for (const workerKey of pool.workerChoiceStrategyContext.workerChoiceStrategies
.get(WorkerChoiceStrategies.FAIR_SHARE)
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
WorkerChoiceStrategies.FAIR_SHARE
- )?.workerLastVirtualTaskTimestamp
- ).toBeUndefined()
+ ).workerLastVirtualTaskTimestamp
+ ).toBeDefined()
pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
for (const workerKey of pool.workerChoiceStrategyContext.workerChoiceStrategies
.get(WorkerChoiceStrategies.FAIR_SHARE)
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
- )?.currentWorkerId
- ).toBeUndefined()
+ ).currentWorkerId
+ ).toBeDefined()
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
- )?.defaultWorkerWeight
- ).toBeUndefined()
+ ).defaultWorkerWeight
+ ).toBeDefined()
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
- )?.workersTaskRunTime
- ).toBeUndefined()
+ ).workersTaskRunTime
+ ).toBeDefined()
pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
- )?.currentWorkerId
- ).toBeUndefined()
+ ).currentWorkerId
+ ).toBeDefined()
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
- )?.defaultWorkerWeight
- ).toBeUndefined()
+ ).defaultWorkerWeight
+ ).toBeDefined()
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
- )?.workersTaskRunTime
- ).toBeUndefined()
+ ).workersTaskRunTime
+ ).toBeDefined()
pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
{ workerChoiceStrategy: 'UNKNOWN_STRATEGY' }
)
).toThrowError(
- new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found")
+ new Error("Invalid worker choice strategy 'UNKNOWN_STRATEGY'")
)
})
})
WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
)
})
-
- it('Verify that getWorkerChoiceStrategy() default return ROUND_ROBIN strategy', () => {
- const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
- fixedPool
- )
- const strategy =
- workerChoiceStrategyContext.getWorkerChoiceStrategy(fixedPool)
- expect(strategy).toBeInstanceOf(RoundRobinWorkerChoiceStrategy)
- })
-
- it('Verify that getWorkerChoiceStrategy() can return ROUND_ROBIN strategy', () => {
- const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
- fixedPool
- )
- const strategy = workerChoiceStrategyContext.getWorkerChoiceStrategy(
- fixedPool,
- WorkerChoiceStrategies.ROUND_ROBIN
- )
- expect(strategy).toBeInstanceOf(RoundRobinWorkerChoiceStrategy)
- })
-
- it('Verify that getWorkerChoiceStrategy() can return LESS_USED strategy', () => {
- const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
- fixedPool
- )
- const strategy = workerChoiceStrategyContext.getWorkerChoiceStrategy(
- fixedPool,
- WorkerChoiceStrategies.LESS_USED
- )
- expect(strategy).toBeInstanceOf(LessUsedWorkerChoiceStrategy)
- })
-
- it('Verify that getWorkerChoiceStrategy() can return LESS_BUSY strategy', () => {
- const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
- fixedPool
- )
- const strategy = workerChoiceStrategyContext.getWorkerChoiceStrategy(
- fixedPool,
- WorkerChoiceStrategies.LESS_BUSY
- )
- expect(strategy).toBeInstanceOf(LessBusyWorkerChoiceStrategy)
- })
-
- it('Verify that getWorkerChoiceStrategy() can return FAIR_SHARE strategy', () => {
- const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
- fixedPool
- )
- const strategy = workerChoiceStrategyContext.getWorkerChoiceStrategy(
- fixedPool,
- WorkerChoiceStrategies.FAIR_SHARE
- )
- expect(strategy).toBeInstanceOf(FairShareWorkerChoiceStrategy)
- })
-
- it('Verify that getWorkerChoiceStrategy() can return WEIGHTED_ROUND_ROBIN strategy', () => {
- const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
- fixedPool
- )
- const strategy = workerChoiceStrategyContext.getWorkerChoiceStrategy(
- fixedPool,
- WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
- )
- expect(strategy).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy)
- })
-
- it('Verify that getWorkerChoiceStrategy() throw error on unknown strategy', () => {
- const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
- fixedPool
- )
- expect(() => {
- workerChoiceStrategyContext.getWorkerChoiceStrategy(
- fixedPool,
- 'UNKNOWN_STRATEGY'
- )
- }).toThrowError(
- new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found")
- )
- })
})