From ff3f866a041b682830639663364c389628095658 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 23 Aug 2023 18:54:05 +0200 Subject: [PATCH] refactor: cleanup tasks queue options namespace MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- CHANGELOG.md | 4 +++ src/index.ts | 3 +- src/pools/abstract-pool.ts | 37 +++++++++++++--------- src/pools/pool.ts | 4 +++ src/utility-types.ts | 2 ++ tests/pools/abstract/abstract-pool.test.js | 16 +++++----- 6 files changed, 42 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ada76405..89b19186 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Changed + +- Rename tasks queue options `queueMaxSize` to `size`. + ## [2.6.32] - 2023-08-23 ### Fixed diff --git a/src/index.ts b/src/index.ts index 6f6c5384..85c3a564 100644 --- a/src/index.ts +++ b/src/index.ts @@ -67,7 +67,8 @@ export type { Task, TaskError, TaskPerformance, - WorkerStatistics + WorkerStatistics, + Writable } from './utility-types' export type { CircularArray } from './circular-array' export type { Deque } from './deque' diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index a4d83b59..3a1a8780 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -5,7 +5,8 @@ import { type TransferListItem } from 'node:worker_threads' import type { MessageValue, PromiseResponseWrapper, - Task + Task, + Writable } from '../utility-types' import { DEFAULT_TASK_NAME, @@ -290,7 +291,7 @@ export abstract class AbstractPool< } private checkValidTasksQueueOptions ( - tasksQueueOptions: TasksQueueOptions + tasksQueueOptions: Writable ): void { if (tasksQueueOptions != null && !isPlainObject(tasksQueueOptions)) { throw new TypeError('Invalid tasks queue options: must be a plain object') @@ -313,18 +314,26 @@ export abstract class AbstractPool< } if ( tasksQueueOptions?.queueMaxSize != null && - !Number.isSafeInteger(tasksQueueOptions.queueMaxSize) + tasksQueueOptions?.size != null ) { - throw new TypeError( - 'Invalid worker node tasks queue max size: must be an integer' + throw new Error( + 'Invalid tasks queue options: cannot specify both queueMaxSize and size' ) } + if (tasksQueueOptions?.queueMaxSize != null) { + tasksQueueOptions.size = tasksQueueOptions.queueMaxSize + } if ( - tasksQueueOptions?.queueMaxSize != null && - tasksQueueOptions.queueMaxSize <= 0 + tasksQueueOptions?.size != null && + !Number.isSafeInteger(tasksQueueOptions.size) ) { + throw new TypeError( + 'Invalid worker node tasks queue max size: must be an integer' + ) + } + if (tasksQueueOptions?.size != null && tasksQueueOptions.size <= 0) { throw new RangeError( - `Invalid worker node tasks queue max size: ${tasksQueueOptions.queueMaxSize} is a negative integer or zero` + `Invalid worker node tasks queue max size: ${tasksQueueOptions.size} is a negative integer or zero` ) } } @@ -641,17 +650,15 @@ export abstract class AbstractPool< this.checkValidTasksQueueOptions(tasksQueueOptions) this.opts.tasksQueueOptions = this.buildTasksQueueOptions(tasksQueueOptions) - this.setTasksQueueMaxSize( - this.opts.tasksQueueOptions.queueMaxSize as number - ) + this.setTasksQueueMaxSize(this.opts.tasksQueueOptions.size as number) } else if (this.opts.tasksQueueOptions != null) { delete this.opts.tasksQueueOptions } } - private setTasksQueueMaxSize (queueMaxSize: number): void { + private setTasksQueueMaxSize (size: number): void { for (const workerNode of this.workerNodes) { - workerNode.tasksQueueBackPressureSize = queueMaxSize + workerNode.tasksQueueBackPressureSize = size } } @@ -660,7 +667,7 @@ export abstract class AbstractPool< ): TasksQueueOptions { return { ...{ - queueMaxSize: Math.pow(this.maxSize, 2), + size: Math.pow(this.maxSize, 2), concurrency: 1 }, ...tasksQueueOptions @@ -1361,7 +1368,7 @@ export abstract class AbstractPool< const workerNode = new WorkerNode( worker, this.worker, - this.opts.tasksQueueOptions?.queueMaxSize ?? Math.pow(this.maxSize, 2) + this.opts.tasksQueueOptions?.size ?? Math.pow(this.maxSize, 2) ) // Flag the worker node as ready at pool startup. if (this.starting) { diff --git a/src/pools/pool.ts b/src/pools/pool.ts index d80ddd7b..fb59d37a 100644 --- a/src/pools/pool.ts +++ b/src/pools/pool.ts @@ -104,6 +104,10 @@ export interface TasksQueueOptions { * * @defaultValue (pool maximum size)^2 */ + readonly size?: number + /** + * @deprecated Use `size` instead. + */ readonly queueMaxSize?: number /** * Maximum number of tasks that can be executed concurrently on a worker node. diff --git a/src/utility-types.ts b/src/utility-types.ts index a418ee88..12067c07 100644 --- a/src/utility-types.ts +++ b/src/utility-types.ts @@ -152,3 +152,5 @@ export interface PromiseResponseWrapper { */ readonly workerNodeKey: number } + +export type Writable = { -readonly [P in keyof T]: T[P] } diff --git a/tests/pools/abstract/abstract-pool.test.js b/tests/pools/abstract/abstract-pool.test.js index 8a8f779c..81250951 100644 --- a/tests/pools/abstract/abstract-pool.test.js +++ b/tests/pools/abstract/abstract-pool.test.js @@ -214,7 +214,7 @@ describe('Abstract pool test suite', () => { expect(pool.opts.enableTasksQueue).toBe(true) expect(pool.opts.tasksQueueOptions).toStrictEqual({ concurrency: 2, - queueMaxSize: 4 + size: 4 }) expect(pool.opts.workerChoiceStrategy).toBe( WorkerChoiceStrategies.LEAST_USED @@ -493,13 +493,13 @@ describe('Abstract pool test suite', () => { expect(pool.opts.enableTasksQueue).toBe(true) expect(pool.opts.tasksQueueOptions).toStrictEqual({ concurrency: 1, - queueMaxSize: 4 + size: 4 }) pool.enableTasksQueue(true, { concurrency: 2 }) expect(pool.opts.enableTasksQueue).toBe(true) expect(pool.opts.tasksQueueOptions).toStrictEqual({ concurrency: 2, - queueMaxSize: 4 + size: 4 }) pool.enableTasksQueue(false) expect(pool.opts.enableTasksQueue).toBe(false) @@ -515,12 +515,12 @@ describe('Abstract pool test suite', () => { ) expect(pool.opts.tasksQueueOptions).toStrictEqual({ concurrency: 1, - queueMaxSize: 4 + size: 4 }) pool.setTasksQueueOptions({ concurrency: 2 }) expect(pool.opts.tasksQueueOptions).toStrictEqual({ concurrency: 2, - queueMaxSize: 4 + size: 4 }) expect(() => pool.setTasksQueueOptions('invalidTasksQueueOptions') @@ -540,17 +540,17 @@ describe('Abstract pool test suite', () => { expect(() => pool.setTasksQueueOptions({ concurrency: 0.2 })).toThrowError( new TypeError('Invalid worker node tasks concurrency: must be an integer') ) - expect(() => pool.setTasksQueueOptions({ queueMaxSize: 0 })).toThrowError( + expect(() => pool.setTasksQueueOptions({ size: 0 })).toThrowError( new RangeError( 'Invalid worker node tasks queue max size: 0 is a negative integer or zero' ) ) - expect(() => pool.setTasksQueueOptions({ queueMaxSize: -1 })).toThrowError( + expect(() => pool.setTasksQueueOptions({ size: -1 })).toThrowError( new RangeError( 'Invalid worker node tasks queue max size: -1 is a negative integer or zero' ) ) - expect(() => pool.setTasksQueueOptions({ queueMaxSize: 0.2 })).toThrowError( + expect(() => pool.setTasksQueueOptions({ size: 0.2 })).toThrowError( new TypeError( 'Invalid worker node tasks queue max size: must be an integer' ) -- 2.34.1