> = new Map<string, PromiseResponseWrapper<Worker, Response>>()
/**
- * Worker choice strategy instance implementing the worker choice algorithm.
+ * Worker choice strategy context referencing a worker choice algorithm implementation.
*
- * Default to a strategy implementing a round robin algorithm.
+ * Default to a round robin algorithm.
*/
protected workerChoiceStrategyContext: WorkerChoiceStrategyContext<
Worker,
public abstract get type (): PoolType
/**
- * Number of tasks concurrently running.
+ * Number of tasks concurrently running in the pool.
*/
private get numberOfRunningTasks (): number {
return this.promiseResponseMap.size
}
}
- /**
- * Removes the given worker from the pool.
- *
- * @param worker - The worker that will be removed.
- */
- protected removeWorker (worker: Worker): void {
- const workerKey = this.getWorkerKey(worker)
- this.workers.splice(workerKey, 1)
- this.workerChoiceStrategyContext.remove(workerKey)
- }
-
/**
* Chooses a worker for the next task.
*
- * The default implementation uses a round robin algorithm to distribute the load.
+ * The default uses a round robin algorithm to distribute the load.
*
* @returns [worker key, worker].
*/
}
/**
- * Pushes the given worker.
+ * Pushes the given worker in the pool.
*
* @param worker - The worker.
* @param tasksUsage - The worker tasks usage.
}
/**
- * Sets the given worker.
+ * Sets the given worker in the pool.
*
* @param workerKey - The worker key.
* @param worker - The worker.
tasksUsage
}
}
+
+ /**
+ * Removes the given worker from the pool.
+ *
+ * @param worker - The worker that will be removed.
+ */
+ protected removeWorker (worker: Worker): void {
+ const workerKey = this.getWorkerKey(worker)
+ this.workers.splice(workerKey, 1)
+ this.workerChoiceStrategyContext.remove(workerKey)
+ }
}
+++ /dev/null
-import type { IPoolInternal } from '../pool-internal'
-import type { IPoolWorker } from '../pool-worker'
-import { FairShareWorkerChoiceStrategy } from './fair-share-worker-choice-strategy'
-import { LessBusyWorkerChoiceStrategy } from './less-busy-worker-choice-strategy'
-import { LessUsedWorkerChoiceStrategy } from './less-used-worker-choice-strategy'
-import { RoundRobinWorkerChoiceStrategy } from './round-robin-worker-choice-strategy'
-import type {
- IWorkerChoiceStrategy,
- WorkerChoiceStrategy
-} from './selection-strategies-types'
-import { WorkerChoiceStrategies } from './selection-strategies-types'
-import { WeightedRoundRobinWorkerChoiceStrategy } from './weighted-round-robin-worker-choice-strategy'
-
-/**
- * Gets the worker choice strategy instance.
- *
- * @param pool - The pool instance.
- * @param workerChoiceStrategy - The worker choice strategy.
- * @returns The worker choice strategy instance.
- */
-export function getWorkerChoiceStrategy<
- Worker extends IPoolWorker,
- Data = unknown,
- Response = unknown
-> (
- 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`
- )
- }
-}
import type { IPoolInternal } from '../pool-internal'
import type { IPoolWorker } from '../pool-worker'
+import { FairShareWorkerChoiceStrategy } from './fair-share-worker-choice-strategy'
+import { LessBusyWorkerChoiceStrategy } from './less-busy-worker-choice-strategy'
+import { LessUsedWorkerChoiceStrategy } from './less-used-worker-choice-strategy'
+import { RoundRobinWorkerChoiceStrategy } from './round-robin-worker-choice-strategy'
import type {
IWorkerChoiceStrategy,
RequiredStatistics,
WorkerChoiceStrategy
} from './selection-strategies-types'
import { WorkerChoiceStrategies } from './selection-strategies-types'
-import { getWorkerChoiceStrategy } from './selection-strategies-utils'
+import { WeightedRoundRobinWorkerChoiceStrategy } from './weighted-round-robin-worker-choice-strategy'
/**
* The worker choice strategy context.
private workerChoiceStrategyType: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
) {
this.execute.bind(this)
- this.workerChoiceStrategy = getWorkerChoiceStrategy<Worker, Data, Response>(
+ this.workerChoiceStrategy = this.getWorkerChoiceStrategy(
pool,
this.workerChoiceStrategyType
)
}
/**
- * Gets the worker choice strategy required statistics.
+ * Gets the worker choice strategy in the context required statistics.
*
* @returns The required statistics.
*/
this.workerChoiceStrategy?.reset()
} else {
this.workerChoiceStrategyType = workerChoiceStrategy
- this.workerChoiceStrategy = getWorkerChoiceStrategy<
- Worker,
- Data,
- Response
- >(pool, this.workerChoiceStrategyType)
+ this.workerChoiceStrategy = this.getWorkerChoiceStrategy(
+ pool,
+ this.workerChoiceStrategyType
+ )
}
}
/**
- * Chooses a worker with the worker choice strategy.
+ * Executes the worker choice strategy algorithm in the context.
*
* @returns The key of the chosen one.
*/
}
/**
- * Removes a worker in the worker choice strategy internals.
+ * Removes a worker from the worker choice strategy in the context.
*
* @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)
}
+
+ /**
+ * 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`
+ )
+ }
+ }
}
+++ /dev/null
-const { expect } = require('expect')
-// const sinon = require('sinon')
-const {
- getWorkerChoiceStrategy
-} = require('../../../lib/pools/selection-strategies/selection-strategies-utils')
-const {
- FixedThreadPool,
- WorkerChoiceStrategies
-} = require('../../../lib/index')
-const {
- RoundRobinWorkerChoiceStrategy
-} = require('../../../lib/pools/selection-strategies/round-robin-worker-choice-strategy')
-const {
- LessUsedWorkerChoiceStrategy
-} = require('../../../lib/pools/selection-strategies/less-used-worker-choice-strategy')
-const {
- LessBusyWorkerChoiceStrategy
-} = require('../../../lib/pools/selection-strategies/less-busy-worker-choice-strategy')
-const {
- FairShareWorkerChoiceStrategy
-} = require('../../../lib/pools/selection-strategies/fair-share-worker-choice-strategy')
-const {
- WeightedRoundRobinWorkerChoiceStrategy
-} = require('../../../lib/pools/selection-strategies/weighted-round-robin-worker-choice-strategy')
-
-describe('Selection strategies utils test suite', () => {
- const max = 3
- let pool
-
- before(() => {
- pool = new FixedThreadPool(max, './tests/worker-files/thread/testWorker.js')
- })
-
- // afterEach(() => {
- // sinon.restore()
- // })
-
- after(async () => {
- await pool.destroy()
- })
-
- it('Verify that getWorkerChoiceStrategy() default return ROUND_ROBIN strategy', () => {
- const strategy = getWorkerChoiceStrategy(pool)
- expect(strategy).toBeInstanceOf(RoundRobinWorkerChoiceStrategy)
- })
-
- it('Verify that getWorkerChoiceStrategy() can return ROUND_ROBIN strategy', () => {
- const strategy = getWorkerChoiceStrategy(
- pool,
- WorkerChoiceStrategies.ROUND_ROBIN
- )
- expect(strategy).toBeInstanceOf(RoundRobinWorkerChoiceStrategy)
- })
-
- it('Verify that getWorkerChoiceStrategy() can return LESS_USED strategy', () => {
- const strategy = getWorkerChoiceStrategy(
- pool,
- WorkerChoiceStrategies.LESS_USED
- )
- expect(strategy).toBeInstanceOf(LessUsedWorkerChoiceStrategy)
- })
-
- it('Verify that getWorkerChoiceStrategy() can return LESS_BUSY strategy', () => {
- const strategy = getWorkerChoiceStrategy(
- pool,
- WorkerChoiceStrategies.LESS_BUSY
- )
- expect(strategy).toBeInstanceOf(LessBusyWorkerChoiceStrategy)
- })
-
- it('Verify that getWorkerChoiceStrategy() can return FAIR_SHARE strategy', () => {
- const strategy = getWorkerChoiceStrategy(
- pool,
- WorkerChoiceStrategies.FAIR_SHARE
- )
- expect(strategy).toBeInstanceOf(FairShareWorkerChoiceStrategy)
- })
-
- it('Verify that getWorkerChoiceStrategy() can return WEIGHTED_ROUND_ROBIN strategy', () => {
- const strategy = getWorkerChoiceStrategy(
- pool,
- WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
- )
- expect(strategy).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy)
- })
-
- it('Verify that getWorkerChoiceStrategy() throw error on unknown strategy', () => {
- expect(() => {
- getWorkerChoiceStrategy(pool, 'UNKNOWN_STRATEGY')
- }).toThrowError(
- new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found")
- )
- })
-})
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")
+ )
+ })
})