From: Jérôme Benoit Date: Mon, 10 Apr 2023 18:00:31 +0000 (+0200) Subject: refactor: factor out worker choice strategies options default X-Git-Tag: v2.4.7~18 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=bbeadd16bc03b9199221a7fb5732af46e7867ded;p=poolifier.git refactor: factor out worker choice strategies options default Signed-off-by: Jérôme Benoit --- diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index a840dc74..6462cb09 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -1,6 +1,10 @@ import crypto from 'node:crypto' import type { MessageValue, PromiseResponseWrapper } from '../utility-types' -import { EMPTY_FUNCTION, median } from '../utils' +import { + DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS, + EMPTY_FUNCTION, + median +} from '../utils' import { KillBehaviors, isKillBehavior } from '../worker/worker-options' import { PoolEvents, type PoolOptions } from './pool' import { PoolEmitter } from './pool' @@ -132,7 +136,7 @@ export abstract class AbstractPool< opts.workerChoiceStrategy ?? WorkerChoiceStrategies.ROUND_ROBIN this.checkValidWorkerChoiceStrategy(this.opts.workerChoiceStrategy) this.opts.workerChoiceStrategyOptions = - opts.workerChoiceStrategyOptions ?? { medRunTime: false } + opts.workerChoiceStrategyOptions ?? DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS this.opts.enableEvents = opts.enableEvents ?? true this.opts.enableTasksQueue = opts.enableTasksQueue ?? false } diff --git a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts index 1da5dc52..d60d2905 100644 --- a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts @@ -1,3 +1,4 @@ +import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils' import type { IPoolInternal } from '../pool-internal' import { PoolType } from '../pool-internal' import type { IWorker } from '../worker' @@ -36,7 +37,7 @@ export abstract class AbstractWorkerChoiceStrategy< */ public constructor ( protected readonly pool: IPoolInternal, - protected readonly opts: WorkerChoiceStrategyOptions = { medRunTime: false } + protected readonly opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS ) { this.checkOptions(this.opts) this.isDynamicPool = this.pool.type === PoolType.DYNAMIC diff --git a/src/pools/selection-strategies/worker-choice-strategy-context.ts b/src/pools/selection-strategies/worker-choice-strategy-context.ts index e763f7b2..486668a2 100644 --- a/src/pools/selection-strategies/worker-choice-strategy-context.ts +++ b/src/pools/selection-strategies/worker-choice-strategy-context.ts @@ -1,3 +1,4 @@ +import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils' import type { IPoolInternal } from '../pool-internal' import type { IWorker } from '../worker' import { FairShareWorkerChoiceStrategy } from './fair-share-worker-choice-strategy' @@ -40,7 +41,7 @@ export class WorkerChoiceStrategyContext< public constructor ( pool: IPoolInternal, private workerChoiceStrategyType: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN, - opts: WorkerChoiceStrategyOptions = { medRunTime: false } + opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS ) { this.execute.bind(this) this.workerChoiceStrategies = new Map< diff --git a/src/utils.ts b/src/utils.ts index d85c5ba5..fc328a6c 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,3 +1,5 @@ +import type { WorkerChoiceStrategyOptions } from './pools/selection-strategies/selection-strategies-types' + /** * An intentional empty function. */ @@ -6,7 +8,15 @@ export const EMPTY_FUNCTION: () => void = Object.freeze(() => { }) /** - * Returns the median of the given data set. + * Default worker choice strategy options. + */ +export const DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS: WorkerChoiceStrategyOptions = + { + medRunTime: false + } + +/** + * Compute the median of the given data set. * * @param dataSet - Data set. * @returns The median of the given data set.