From: Jérôme Benoit Date: Fri, 7 Apr 2023 16:46:42 +0000 (+0200) Subject: refactor: untangle worker choosing code from worker creation code X-Git-Tag: v2.4.4~7 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=17393ac83cbf0eacf68ea9efb0a577cc30a5c685;p=poolifier.git refactor: untangle worker choosing code from worker creation code Signed-off-by: Jérôme Benoit --- diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 97225246..2e033fc1 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -93,23 +93,7 @@ export abstract class AbstractPool< Worker, Data, Response - >( - this, - () => { - const createdWorker = this.createAndSetupWorker() - this.registerWorkerMessageListener(createdWorker, message => { - if ( - isKillBehavior(KillBehaviors.HARD, message.kill) || - this.getWorkerTasksUsage(createdWorker)?.running === 0 - ) { - // Kill received from the worker, means that no new tasks are submitted to that worker for a while ( > maxInactiveTime) - void this.destroyWorker(createdWorker) - } - }) - return this.getWorkerKey(createdWorker) - }, - this.opts.workerChoiceStrategy - ) + >(this, this.opts.workerChoiceStrategy) } private checkFilePath (filePath: string): void { @@ -308,7 +292,26 @@ export abstract class AbstractPool< * @returns [worker key, worker]. */ protected chooseWorker (): [number, Worker] { - const workerKey = this.workerChoiceStrategyContext.execute() + let workerKey: number + if ( + this.type === PoolType.DYNAMIC && + !this.full && + this.findFreeWorkerKey() === -1 + ) { + const createdWorker = this.createAndSetupWorker() + this.registerWorkerMessageListener(createdWorker, message => { + if ( + isKillBehavior(KillBehaviors.HARD, message.kill) || + this.getWorkerTasksUsage(createdWorker)?.running === 0 + ) { + // Kill received from the worker, means that no new tasks are submitted to that worker for a while ( > maxInactiveTime) + void this.destroyWorker(createdWorker) + } + }) + workerKey = this.getWorkerKey(createdWorker) + } else { + workerKey = this.workerChoiceStrategyContext.execute() + } return [workerKey, this.workers[workerKey].worker] } diff --git a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts index 0d337b53..f32229ec 100644 --- a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts @@ -17,7 +17,7 @@ export abstract class AbstractWorkerChoiceStrategy< Worker extends IPoolWorker, Data = unknown, Response = unknown -> implements IWorkerChoiceStrategy { +> implements IWorkerChoiceStrategy { /** @inheritDoc */ public readonly isDynamicPool: boolean /** @inheritDoc */ diff --git a/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts b/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts index f8427d01..16d5a7be 100644 --- a/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts @@ -27,7 +27,7 @@ export class FairShareWorkerChoiceStrategy< Response = unknown > extends AbstractWorkerChoiceStrategy - implements IWorkerChoiceStrategy { + implements IWorkerChoiceStrategy { /** @inheritDoc */ public readonly requiredStatistics: RequiredStatistics = { runTime: true, diff --git a/src/pools/selection-strategies/less-busy-worker-choice-strategy.ts b/src/pools/selection-strategies/less-busy-worker-choice-strategy.ts index a9c55004..87ef804d 100644 --- a/src/pools/selection-strategies/less-busy-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/less-busy-worker-choice-strategy.ts @@ -18,7 +18,7 @@ export class LessBusyWorkerChoiceStrategy< Response = unknown > extends AbstractWorkerChoiceStrategy - implements IWorkerChoiceStrategy { + implements IWorkerChoiceStrategy { /** @inheritDoc */ public readonly requiredStatistics: RequiredStatistics = { runTime: true, diff --git a/src/pools/selection-strategies/less-used-worker-choice-strategy.ts b/src/pools/selection-strategies/less-used-worker-choice-strategy.ts index f3b45e06..364c54a1 100644 --- a/src/pools/selection-strategies/less-used-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/less-used-worker-choice-strategy.ts @@ -15,7 +15,7 @@ export class LessUsedWorkerChoiceStrategy< Response = unknown > extends AbstractWorkerChoiceStrategy - implements IWorkerChoiceStrategy { + implements IWorkerChoiceStrategy { /** @inheritDoc */ public reset (): boolean { return true diff --git a/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts b/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts index 9cc966c4..29b05fa5 100644 --- a/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts @@ -15,7 +15,7 @@ export class RoundRobinWorkerChoiceStrategy< Response = unknown > extends AbstractWorkerChoiceStrategy - implements IWorkerChoiceStrategy { + implements IWorkerChoiceStrategy { /** * Id of the next worker. */ diff --git a/src/pools/selection-strategies/selection-strategies-types.ts b/src/pools/selection-strategies/selection-strategies-types.ts index 137c1be7..9acf819b 100644 --- a/src/pools/selection-strategies/selection-strategies-types.ts +++ b/src/pools/selection-strategies/selection-strategies-types.ts @@ -1,6 +1,3 @@ -import type { IPoolInternal } from '../pool-internal' -import type { IPoolWorker } from '../pool-worker' - /** * Enumeration of worker choice strategies. */ @@ -43,15 +40,7 @@ export interface RequiredStatistics { /** * Worker choice strategy interface. */ -export interface IWorkerChoiceStrategy< - Worker extends IPoolWorker, - Data = unknown, - Response = unknown -> { - /** - * The pool instance. - */ - readonly pool: IPoolInternal +export interface IWorkerChoiceStrategy { /** * Is the pool bound to the strategy dynamic?. */ diff --git a/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts b/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts index 997366cd..ea598b20 100644 --- a/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts @@ -29,7 +29,7 @@ export class WeightedRoundRobinWorkerChoiceStrategy< Response = unknown > extends AbstractWorkerChoiceStrategy - implements IWorkerChoiceStrategy { + implements IWorkerChoiceStrategy { /** @inheritDoc */ public readonly requiredStatistics: RequiredStatistics = { runTime: true, diff --git a/src/pools/selection-strategies/worker-choice-strategy-context.ts b/src/pools/selection-strategies/worker-choice-strategy-context.ts index 3fbf5e23..66aafad6 100644 --- a/src/pools/selection-strategies/worker-choice-strategy-context.ts +++ b/src/pools/selection-strategies/worker-choice-strategy-context.ts @@ -26,7 +26,7 @@ export class WorkerChoiceStrategyContext< > { private readonly workerChoiceStrategies: Map< WorkerChoiceStrategy, - IWorkerChoiceStrategy + IWorkerChoiceStrategy > /** @@ -38,13 +38,12 @@ export class WorkerChoiceStrategyContext< */ public constructor ( pool: IPoolInternal, - private readonly createWorkerCallback: () => number, private workerChoiceStrategyType: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN ) { this.execute.bind(this) this.workerChoiceStrategies = new Map< WorkerChoiceStrategy, - IWorkerChoiceStrategy + IWorkerChoiceStrategy >([ [ WorkerChoiceStrategies.ROUND_ROBIN, @@ -78,7 +77,7 @@ export class WorkerChoiceStrategyContext< return ( this.workerChoiceStrategies.get( this.workerChoiceStrategyType - ) as IWorkerChoiceStrategy + ) as IWorkerChoiceStrategy ).requiredStatistics } @@ -103,17 +102,11 @@ export class WorkerChoiceStrategyContext< * @returns The key of the chosen one. */ public execute (): number { - const workerChoiceStrategy = this.workerChoiceStrategies.get( - this.workerChoiceStrategyType - ) as IWorkerChoiceStrategy - if ( - workerChoiceStrategy.isDynamicPool && - !workerChoiceStrategy.pool.full && - workerChoiceStrategy.pool.findFreeWorkerKey() === -1 - ) { - return this.createWorkerCallback() - } - return workerChoiceStrategy.choose() + return ( + this.workerChoiceStrategies.get( + this.workerChoiceStrategyType + ) as IWorkerChoiceStrategy + ).choose() } /** @@ -126,7 +119,7 @@ export class WorkerChoiceStrategyContext< return ( this.workerChoiceStrategies.get( this.workerChoiceStrategyType - ) as IWorkerChoiceStrategy + ) as IWorkerChoiceStrategy ).remove(workerKey) } }