From: Jérôme Benoit Date: Tue, 29 Aug 2023 19:00:31 +0000 (+0200) Subject: refactor: cleanup interface scope X-Git-Tag: v2.6.38~4 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=5b49e86408b36f63d5076a086e2feca64a9690d0;p=poolifier.git refactor: cleanup interface scope Signed-off-by: Jérôme Benoit --- diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 0d28cffe..493c5ccc 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -655,13 +655,13 @@ export abstract class AbstractPool< this.checkValidTasksQueueOptions(tasksQueueOptions) this.opts.tasksQueueOptions = this.buildTasksQueueOptions(tasksQueueOptions) - this.setTasksQueueMaxSize(this.opts.tasksQueueOptions.size as number) + this.setTasksQueueSize(this.opts.tasksQueueOptions.size as number) } else if (this.opts.tasksQueueOptions != null) { delete this.opts.tasksQueueOptions } } - private setTasksQueueMaxSize (size: number): void { + private setTasksQueueSize (size: number): void { for (const workerNode of this.workerNodes) { workerNode.tasksQueueBackPressureSize = size } diff --git a/src/pools/worker-node.ts b/src/pools/worker-node.ts index f9b4016c..387d05f9 100644 --- a/src/pools/worker-node.ts +++ b/src/pools/worker-node.ts @@ -53,19 +53,7 @@ implements IWorkerNode { * @param tasksQueueBackPressureSize - The tasks queue back pressure size. */ constructor (worker: Worker, tasksQueueBackPressureSize: number) { - if (worker == null) { - throw new TypeError('Cannot construct a worker node without a worker') - } - if (tasksQueueBackPressureSize == null) { - throw new TypeError( - 'Cannot construct a worker node without a tasks queue back pressure size' - ) - } - if (!Number.isSafeInteger(tasksQueueBackPressureSize)) { - throw new TypeError( - 'Cannot construct a worker node with a tasks queue back pressure size that is not an integer' - ) - } + this.checkWorkerNodeArguments(worker, tasksQueueBackPressureSize) this.worker = worker this.info = this.initWorkerInfo(worker) this.usage = this.initWorkerUsage() @@ -270,4 +258,28 @@ implements IWorkerNode { } } } + + private checkWorkerNodeArguments ( + worker: Worker, + tasksQueueBackPressureSize: number + ): void { + if (worker == null) { + throw new TypeError('Cannot construct a worker node without a worker') + } + if (tasksQueueBackPressureSize == null) { + throw new TypeError( + 'Cannot construct a worker node without a tasks queue back pressure size' + ) + } + if (!Number.isSafeInteger(tasksQueueBackPressureSize)) { + throw new TypeError( + 'Cannot construct a worker node with a tasks queue back pressure size that is not an integer' + ) + } + if (tasksQueueBackPressureSize <= 0) { + throw new RangeError( + 'Cannot construct a worker node with a tasks queue back pressure size that is not a positive integer' + ) + } + } } diff --git a/src/pools/worker.ts b/src/pools/worker.ts index be134f05..5cd58b28 100644 --- a/src/pools/worker.ts +++ b/src/pools/worker.ts @@ -132,7 +132,7 @@ export interface WorkerInfo { /** * Worker type. */ - type: WorkerType + readonly type: WorkerType /** * Dynamic flag. */ @@ -199,6 +199,12 @@ export interface IWorker { readonly once: (event: 'exit', handler: ExitHandler) => void } +/** + * Worker node event callback. + * + * @param workerId - The worker id. + * @internal + */ export type WorkerNodeEventCallback = (workerId: number) => void /** @@ -220,7 +226,7 @@ export interface IWorkerNode { /** * Worker usage statistics. */ - usage: WorkerUsage + readonly usage: WorkerUsage /** * Message channel (worker_threads only). */ @@ -232,14 +238,10 @@ export interface IWorkerNode { tasksQueueBackPressureSize: number /** * Callback invoked when worker node tasks queue is back pressured. - * - * @param workerId - The worker id. */ onBackPressure?: WorkerNodeEventCallback /** * Callback invoked when worker node tasks queue is empty. - * - * @param workerId - The worker id. */ onEmptyQueue?: WorkerNodeEventCallback /** diff --git a/src/utility-types.ts b/src/utility-types.ts index 12067c07..e1fb311e 100644 --- a/src/utility-types.ts +++ b/src/utility-types.ts @@ -47,13 +47,19 @@ export interface TaskPerformance { } /** - * Performance statistics computation. + * Worker task performance statistics computation settings. * * @internal */ export interface WorkerStatistics { - runTime: boolean - elu: boolean + /** + * Whether the worker computes the task runtime or not. + */ + readonly runTime: boolean + /** + * Whether the worker computes the task event loop utilization (ELU) or not. + */ + readonly elu: boolean } /** diff --git a/tests/pools/abstract/worker-node.test.js b/tests/pools/abstract/worker-node.test.js index df4e03fe..4b325f43 100644 --- a/tests/pools/abstract/worker-node.test.js +++ b/tests/pools/abstract/worker-node.test.js @@ -29,6 +29,21 @@ describe('Worker node test suite', () => { 'Cannot construct a worker node with a tasks queue back pressure size that is not an integer' ) ) + expect(() => new WorkerNode(threadWorker, 0.2)).toThrowError( + new TypeError( + 'Cannot construct a worker node with a tasks queue back pressure size that is not an integer' + ) + ) + expect(() => new WorkerNode(threadWorker, 0)).toThrowError( + new RangeError( + 'Cannot construct a worker node with a tasks queue back pressure size that is not a positive integer' + ) + ) + expect(() => new WorkerNode(threadWorker, -1)).toThrowError( + new RangeError( + 'Cannot construct a worker node with a tasks queue back pressure size that is not a positive integer' + ) + ) expect(threadWorkerNode).toBeInstanceOf(WorkerNode) expect(threadWorkerNode.worker).toBe(threadWorker) expect(threadWorkerNode.info).toStrictEqual({