refactor: cleanup tasks queue options namespace
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 23 Aug 2023 16:54:05 +0000 (18:54 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 23 Aug 2023 16:54:05 +0000 (18:54 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
CHANGELOG.md
src/index.ts
src/pools/abstract-pool.ts
src/pools/pool.ts
src/utility-types.ts
tests/pools/abstract/abstract-pool.test.js

index ada76405843e1a774cf6ce54cab0d481d0e5cae3..89b1918650b9c7e49de75fa9dcefa1aa42637674 100644 (file)
@@ -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
index 6f6c53849162e84377d7c6380c1c29b3e499d127..85c3a564af238e2858517105ec2960461f41c0a4 100644 (file)
@@ -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'
index a4d83b593cd52b4927aa480f557227e788d6cfeb..3a1a87807cc065df871bb1ca6d2195619672a64d 100644 (file)
@@ -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<TasksQueueOptions>
   ): 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, Data>(
       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) {
index d80ddd7b2cb810c987c64dbcc40338d3b89735ab..fb59d37a19892e8eb8cb979bbc69454eaae4a35b 100644 (file)
@@ -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.
index a418ee885f903d3bb9838c0c34be0e6bbce43524..12067c077e06599339a0b01e0ebc51df42178713 100644 (file)
@@ -152,3 +152,5 @@ export interface PromiseResponseWrapper<Response = unknown> {
    */
   readonly workerNodeKey: number
 }
+
+export type Writable<T> = { -readonly [P in keyof T]: T[P] }
index 8a8f779c41603f09859bf475d0c55406a04fa918..812509513bffee46808b739d1fb7422e8c8eb3b7 100644 (file)
@@ -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'
       )