From: Jérôme Benoit Date: Tue, 19 Nov 2024 14:29:56 +0000 (+0100) Subject: refactor: flag some object literals as read only X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=7cec62a452c0d44aa4494ff602bf4f6ee6e2be00;p=poolifier.git refactor: flag some object literals as read only Signed-off-by: Jérôme Benoit --- diff --git a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts index 69cb0bbc..9d1a62e0 100644 --- a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts @@ -41,11 +41,12 @@ export abstract class AbstractWorkerChoiceStrategy< } /** @inheritDoc */ - public readonly taskStatisticsRequirements: TaskStatisticsRequirements = { - elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS, - runTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS, - waitTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS, - } + public readonly taskStatisticsRequirements: TaskStatisticsRequirements = + Object.freeze({ + elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS, + runTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS, + waitTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS, + }) /** * Constructs a worker choice strategy bound to the pool. 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 23f751df..fc0dcc3b 100644 --- a/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts @@ -25,7 +25,7 @@ export class FairShareWorkerChoiceStrategy< implements IWorkerChoiceStrategy { /** @inheritDoc */ public override readonly taskStatisticsRequirements: TaskStatisticsRequirements = - { + Object.freeze({ elu: { aggregate: true, average: true, @@ -41,7 +41,7 @@ export class FairShareWorkerChoiceStrategy< average: true, median: false, }, - } + }) /** @inheritDoc */ public constructor ( diff --git a/src/pools/selection-strategies/interleaved-weighted-round-robin-worker-choice-strategy.ts b/src/pools/selection-strategies/interleaved-weighted-round-robin-worker-choice-strategy.ts index 0db54c4f..fbc79718 100644 --- a/src/pools/selection-strategies/interleaved-weighted-round-robin-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/interleaved-weighted-round-robin-worker-choice-strategy.ts @@ -41,7 +41,7 @@ export class InterleavedWeightedRoundRobinWorkerChoiceStrategy< private workerNodeVirtualTaskExecutionTime = 0 /** @inheritDoc */ public override readonly taskStatisticsRequirements: TaskStatisticsRequirements = - { + Object.freeze({ elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS, runTime: { aggregate: true, @@ -53,7 +53,7 @@ export class InterleavedWeightedRoundRobinWorkerChoiceStrategy< average: true, median: false, }, - } + }) /** @inheritDoc */ public constructor ( diff --git a/src/pools/selection-strategies/least-busy-worker-choice-strategy.ts b/src/pools/selection-strategies/least-busy-worker-choice-strategy.ts index 5392cdd2..b6ad7075 100644 --- a/src/pools/selection-strategies/least-busy-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/least-busy-worker-choice-strategy.ts @@ -24,7 +24,7 @@ export class LeastBusyWorkerChoiceStrategy< implements IWorkerChoiceStrategy { /** @inheritDoc */ public override readonly taskStatisticsRequirements: TaskStatisticsRequirements = - { + Object.freeze({ elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS, runTime: { aggregate: true, @@ -36,7 +36,7 @@ export class LeastBusyWorkerChoiceStrategy< average: false, median: false, }, - } + }) /** @inheritDoc */ public constructor ( diff --git a/src/pools/selection-strategies/least-elu-worker-choice-strategy.ts b/src/pools/selection-strategies/least-elu-worker-choice-strategy.ts index 8b0548c4..37193a1c 100644 --- a/src/pools/selection-strategies/least-elu-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/least-elu-worker-choice-strategy.ts @@ -24,7 +24,7 @@ export class LeastEluWorkerChoiceStrategy< implements IWorkerChoiceStrategy { /** @inheritDoc */ public override readonly taskStatisticsRequirements: TaskStatisticsRequirements = - { + Object.freeze({ elu: { aggregate: true, average: false, @@ -32,7 +32,7 @@ export class LeastEluWorkerChoiceStrategy< }, runTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS, waitTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS, - } + }) /** @inheritDoc */ public constructor ( 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 b3a4f1d8..a62103a9 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 @@ -30,7 +30,7 @@ export class WeightedRoundRobinWorkerChoiceStrategy< /** @inheritDoc */ public override readonly taskStatisticsRequirements: TaskStatisticsRequirements = - { + Object.freeze({ elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS, runTime: { aggregate: true, @@ -42,7 +42,7 @@ export class WeightedRoundRobinWorkerChoiceStrategy< average: true, median: false, }, - } + }) /** @inheritDoc */ public constructor ( diff --git a/src/pools/utils.ts b/src/pools/utils.ts index 3c88dd45..c8257eaf 100644 --- a/src/pools/utils.ts +++ b/src/pools/utils.ts @@ -30,24 +30,24 @@ import { /** * Default measurement statistics requirements. */ -export const DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS: MeasurementStatisticsRequirements = - { +export const DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS: Readonly = + Object.freeze({ aggregate: false, average: false, median: false, - } + }) export const getDefaultTasksQueueOptions = ( poolMaxSize: number -): Required => { - return { +): Required> => { + return Object.freeze({ concurrency: 1, size: Math.pow(poolMaxSize, 2), tasksFinishedTimeout: 2000, tasksStealingOnBackPressure: true, tasksStealingRatio: 0.6, taskStealing: true, - } + }) } export const checkFilePath = (filePath: string | undefined): void => { diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index c8aa5a05..1ea46a23 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -34,7 +34,7 @@ import { import { KillBehaviors, type WorkerOptions } from './worker-options.js' const DEFAULT_MAX_INACTIVE_TIME = 60000 -const DEFAULT_WORKER_OPTIONS: WorkerOptions = { +const DEFAULT_WORKER_OPTIONS: Readonly = Object.freeze({ /** * The kill behavior option on this worker or its default value. */ @@ -48,7 +48,7 @@ const DEFAULT_WORKER_OPTIONS: WorkerOptions = { * The pool automatically checks and terminates this worker when the time expires. */ maxInactiveTime: DEFAULT_MAX_INACTIVE_TIME, -} +}) /** * Base class that implements some shared logic for all poolifier workers. @@ -68,7 +68,7 @@ export abstract class AbstractWorker< /** * Worker id. */ - protected abstract id: number + protected abstract readonly id: number /** * Timestamp of the last task processed by this worker. */ @@ -194,7 +194,7 @@ export abstract class AbstractWorker< protected readonly isMain: boolean | undefined, private readonly mainWorker: MainWorker | null | undefined, taskFunctions: TaskFunction | TaskFunctions, - protected opts: WorkerOptions = DEFAULT_WORKER_OPTIONS + protected readonly opts: WorkerOptions = DEFAULT_WORKER_OPTIONS ) { if (this.isMain == null) { throw new Error('isMain parameter is mandatory')