]> Piment Noir Git Repositories - poolifier.git/commitdiff
refactor: coding style cleanups
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Tue, 1 Jul 2025 19:53:49 +0000 (21:53 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Tue, 1 Jul 2025 19:53:49 +0000 (21:53 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
20 files changed:
benchmarks/benchmarks-utils.cjs
benchmarks/worker-selection/least.mjs
examples/typescript/http-server-pool/express-cluster/src/worker.ts
examples/typescript/http-server-pool/express-hybrid/src/request-handler-worker.ts
examples/typescript/http-server-pool/express-worker_threads/src/worker.ts
examples/typescript/http-server-pool/fastify-cluster/src/worker.ts
examples/typescript/http-server-pool/fastify-hybrid/src/request-handler-worker.ts
examples/typescript/http-server-pool/fastify-worker_threads/src/worker.ts
examples/typescript/websocket-server-pool/ws-cluster/src/worker.ts
examples/typescript/websocket-server-pool/ws-hybrid/src/request-handler-worker.ts
examples/typescript/websocket-server-pool/ws-worker_threads/src/worker.ts
src/pools/abstract-pool.ts
src/pools/selection-strategies/selection-strategies-utils.ts
src/pools/utils.ts
src/queues/priority-queue.ts
src/utils.ts
src/worker/abstract-worker.ts
tests/pools/abstract-pool.test.mjs
tests/pools/utils.test.mjs
tests/test-utils.cjs

index 2f401e8055f6bbc212a355f9f0a0b6f700a05a93..fa266a7538af96a0726b03124abebfc870aa14db 100644 (file)
@@ -43,15 +43,14 @@ const fibonacci = n => {
 const factorial = n => {
   if (n === 0 || n === 1) {
     return 1n
-  } else {
-    n = BigInt(n)
-    let factorial = 1n
-    for (let i = 1n; i <= n; i++) {
-      factorial *= i
-    }
-    // cluster worker do not support BigInt
-    return factorial.toString()
   }
+  n = BigInt(n)
+  let factorial = 1n
+  for (let i = 1n; i <= n; i++) {
+    factorial *= i
+  }
+  // cluster worker do not support BigInt
+  return factorial.toString()
 }
 
 const readWriteFiles = (
index 05e3dc6b1c4f7de4f43f398a27e778a260f626a8..3b954b7f23bdf1355d3e80cfd785f586e5dcf95b 100644 (file)
@@ -31,7 +31,8 @@ function arraySortSelect (tasksMap) {
   return tasksArray.sort((a, b) => {
     if (a[1] < b[1]) {
       return -1
-    } else if (a[1] > b[1]) {
+    }
+    if (a[1] > b[1]) {
       return 1
     }
     return 0
@@ -49,7 +50,8 @@ function loopSelect (tasksMap) {
   for (const [key, value] of tasksMap) {
     if (value === 0) {
       return key
-    } else if (value < minValue) {
+    }
+    if (value < minValue) {
       minKey = key
       minValue = value
     }
@@ -188,7 +190,8 @@ function selectLoop (
     pivotIndex = partition(array, leftIndex, rightIndex, pivotIndex, compare)
     if (k === pivotIndex) {
       return array[k]
-    } else if (k < pivotIndex) {
+    }
+    if (k < pivotIndex) {
       rightIndex = pivotIndex - 1
     } else {
       leftIndex = pivotIndex + 1
@@ -219,11 +222,11 @@ function selectRecursion (
   pivotIndex = partition(array, leftIndex, rightIndex, pivotIndex, compare)
   if (k === pivotIndex) {
     return array[k]
-  } else if (k < pivotIndex) {
+  }
+  if (k < pivotIndex) {
     return selectRecursion(array, k, leftIndex, pivotIndex - 1, compare)
-  } else {
-    return selectRecursion(array, k, pivotIndex + 1, rightIndex, k, compare)
   }
+  return selectRecursion(array, k, pivotIndex + 1, rightIndex, k, compare)
 }
 
 /**
index 5f70b1da693d0cfce09f0a390cce55767370c583..40158ac92f65cb66bd591a62718aeecf0c5d4eb4 100644 (file)
@@ -20,14 +20,13 @@ class ExpressWorker extends ClusterWorker<WorkerData, WorkerResponse> {
   private static readonly factorial = (n: bigint | number): bigint => {
     if (n === 0 || n === 1) {
       return 1n
-    } else {
-      n = BigInt(n)
-      let factorial = 1n
-      for (let i = 1n; i <= n; i++) {
-        factorial *= i
-      }
-      return factorial
     }
+    n = BigInt(n)
+    let factorial = 1n
+    for (let i = 1n; i <= n; i++) {
+      factorial *= i
+    }
+    return factorial
   }
 
   private static readonly startExpress = (
index 5edaddad78564be199f1e940eda80ff93af21b77..4a31133bcdfa2ff433210b8d89d05361d3ca4d96 100644 (file)
@@ -31,14 +31,13 @@ class RequestHandlerWorker<
   private static readonly factorial = (n: bigint | number): bigint => {
     if (n === 0 || n === 1) {
       return 1n
-    } else {
-      n = BigInt(n)
-      let factorial = 1n
-      for (let i = 1n; i <= n; i++) {
-        factorial *= i
-      }
-      return factorial
     }
+    n = BigInt(n)
+    let factorial = 1n
+    for (let i = 1n; i <= n; i++) {
+      factorial *= i
+    }
+    return factorial
   }
 }
 
index 9796ccb7ef7bae5ec398af8b9695d65a3deaf8d3..b01283d327c06a69315a22c2bc7de2cce4b4259d 100644 (file)
@@ -27,14 +27,13 @@ class RequestHandlerWorker<
   private static readonly factorial: (n: bigint | number) => bigint = n => {
     if (n === 0 || n === 1) {
       return 1n
-    } else {
-      n = BigInt(n)
-      let factorial = 1n
-      for (let i = 1n; i <= n; i++) {
-        factorial *= i
-      }
-      return factorial
     }
+    n = BigInt(n)
+    let factorial = 1n
+    for (let i = 1n; i <= n; i++) {
+      factorial *= i
+    }
+    return factorial
   }
 }
 
index 7ef976370ccd3fc3955b2fc30519492d445774df..7bf360b818b9f54b0f196ef2108e3e6edfa30ca6 100644 (file)
@@ -19,14 +19,13 @@ class FastifyWorker extends ClusterWorker<WorkerData, WorkerResponse> {
   private static readonly factorial = (n: bigint | number): bigint => {
     if (n === 0 || n === 1) {
       return 1n
-    } else {
-      n = BigInt(n)
-      let factorial = 1n
-      for (let i = 1n; i <= n; i++) {
-        factorial *= i
-      }
-      return factorial
     }
+    n = BigInt(n)
+    let factorial = 1n
+    for (let i = 1n; i <= n; i++) {
+      factorial *= i
+    }
+    return factorial
   }
 
   private static readonly startFastify = async (
index 5edaddad78564be199f1e940eda80ff93af21b77..4a31133bcdfa2ff433210b8d89d05361d3ca4d96 100644 (file)
@@ -31,14 +31,13 @@ class RequestHandlerWorker<
   private static readonly factorial = (n: bigint | number): bigint => {
     if (n === 0 || n === 1) {
       return 1n
-    } else {
-      n = BigInt(n)
-      let factorial = 1n
-      for (let i = 1n; i <= n; i++) {
-        factorial *= i
-      }
-      return factorial
     }
+    n = BigInt(n)
+    let factorial = 1n
+    for (let i = 1n; i <= n; i++) {
+      factorial *= i
+    }
+    return factorial
   }
 }
 
index 9796ccb7ef7bae5ec398af8b9695d65a3deaf8d3..b01283d327c06a69315a22c2bc7de2cce4b4259d 100644 (file)
@@ -27,14 +27,13 @@ class RequestHandlerWorker<
   private static readonly factorial: (n: bigint | number) => bigint = n => {
     if (n === 0 || n === 1) {
       return 1n
-    } else {
-      n = BigInt(n)
-      let factorial = 1n
-      for (let i = 1n; i <= n; i++) {
-        factorial *= i
-      }
-      return factorial
     }
+    n = BigInt(n)
+    let factorial = 1n
+    for (let i = 1n; i <= n; i++) {
+      factorial *= i
+    }
+    return factorial
   }
 }
 
index 4a0ae6a86f74b28e376b30aded5a15bf7e89a83d..eca9cd9fb172f7d9edcb46630e779630f434f0b9 100644 (file)
@@ -23,14 +23,13 @@ class WebSocketServerWorker extends ClusterWorker<WorkerData, WorkerResponse> {
   private static readonly factorial = (n: bigint | number): bigint => {
     if (n === 0 || n === 1) {
       return 1n
-    } else {
-      n = BigInt(n)
-      let factorial = 1n
-      for (let i = 1n; i <= n; i++) {
-        factorial *= i
-      }
-      return factorial
     }
+    n = BigInt(n)
+    let factorial = 1n
+    for (let i = 1n; i <= n; i++) {
+      factorial *= i
+    }
+    return factorial
   }
 
   private static readonly startWebSocketServer = (
@@ -49,6 +48,7 @@ class WebSocketServerWorker extends ClusterWorker<WorkerData, WorkerResponse> {
       ws.on('error', console.error)
       ws.on('message', (message: RawData) => {
         const { data, type } = JSON.parse(
+          // eslint-disable-next-line @typescript-eslint/no-base-to-string
           message.toString()
         ) as MessagePayload<DataPayload>
         switch (type) {
index 89f53f9d36b2f6363686c321692f510a9a76d50c..68c981efeecb5f336e735fee8814c3bdf120343f 100644 (file)
@@ -29,14 +29,13 @@ class RequestHandlerWorker<
   private static readonly factorial = (n: bigint | number): bigint => {
     if (n === 0 || n === 1) {
       return 1n
-    } else {
-      n = BigInt(n)
-      let factorial = 1n
-      for (let i = 1n; i <= n; i++) {
-        factorial *= i
-      }
-      return factorial
     }
+    n = BigInt(n)
+    let factorial = 1n
+    for (let i = 1n; i <= n; i++) {
+      factorial *= i
+    }
+    return factorial
   }
 }
 
index 9bc8ed659730dbbb4c26c46e925f9c9d5074ee49..b04d12188cb920425b1474ac955093897d77e789 100644 (file)
@@ -25,14 +25,13 @@ class RequestHandlerWorker<
   private static readonly factorial = (n: bigint | number): bigint => {
     if (n === 0 || n === 1) {
       return 1n
-    } else {
-      n = BigInt(n)
-      let factorial = 1n
-      for (let i = 1n; i <= n; i++) {
-        factorial *= i
-      }
-      return factorial
     }
+    n = BigInt(n)
+    let factorial = 1n
+    for (let i = 1n; i <= n; i++) {
+      factorial *= i
+    }
+    return factorial
   }
 }
 
index 60f350645de0fda4de7f503e7482d07358bad519..639a967e7defa0dd984cd206e40a4d02f4301609 100644 (file)
@@ -1470,7 +1470,8 @@ export abstract class AbstractPool<
   private checkMessageWorkerId (message: MessageValue<Data | Response>): void {
     if (message.workerId == null) {
       throw new Error('Worker message received without worker id')
-    } else if (this.getWorkerNodeKeyByWorkerId(message.workerId) === -1) {
+    }
+    if (this.getWorkerNodeKeyByWorkerId(message.workerId) === -1) {
       throw new Error(
         `Worker message received from unknown worker '${message.workerId.toString()}'`
       )
@@ -1484,15 +1485,18 @@ export abstract class AbstractPool<
       throw new Error(
         'Cannot instantiate a pool without specifying the number of workers'
       )
-    } else if (!Number.isSafeInteger(minimumNumberOfWorkers)) {
+    }
+    if (!Number.isSafeInteger(minimumNumberOfWorkers)) {
       throw new TypeError(
         'Cannot instantiate a pool with a non safe integer number of workers'
       )
-    } else if (minimumNumberOfWorkers < 0) {
+    }
+    if (minimumNumberOfWorkers < 0) {
       throw new RangeError(
         'Cannot instantiate a pool with a negative number of workers'
       )
-    } else if (this.type === PoolTypes.fixed && minimumNumberOfWorkers === 0) {
+    }
+    if (this.type === PoolTypes.fixed && minimumNumberOfWorkers === 0) {
       throw new RangeError('Cannot instantiate a fixed pool with zero worker')
     }
   }
@@ -2242,7 +2246,7 @@ export abstract class AbstractPool<
     message: MessageValue<Data>
   ): Promise<boolean> {
     return await new Promise<boolean>((resolve, reject) => {
-      const responsesReceived = new Array<MessageValue<Response>>()
+      const responsesReceived: MessageValue<Response>[] = []
       const taskFunctionOperationsListener = (
         message: MessageValue<Response>
       ): void => {
index f4268a0b6a848cd3a8d2fde4bac63a22b515d058..8fabdcff40dab18b895f308875bb9a061b5e3068 100644 (file)
@@ -50,8 +50,8 @@ const getDefaultWorkerWeight = (): number => {
     }
     // CPU estimated cycle time
     const numberOfDigits = cpu.speed.toString().length - 1
-    const cpuCycleTime = 1 / (cpu.speed / Math.pow(10, numberOfDigits))
-    cpusCycleTimeWeight += cpuCycleTime * Math.pow(10, numberOfDigits)
+    const cpuCycleTime = 1 / (cpu.speed / 10 ** numberOfDigits)
+    cpusCycleTimeWeight += cpuCycleTime * 10 ** numberOfDigits
   }
   return Math.round(cpusCycleTimeWeight / currentCpus.length)
 }
index d708a5cf5b00a0caf314d815c1b30d8e370821e2..3b9ecaddda0832d31238be3e8d8100cd272c44e7 100644 (file)
@@ -43,7 +43,7 @@ export const getDefaultTasksQueueOptions = (
 ): Required<Readonly<TasksQueueOptions>> => {
   return Object.freeze({
     concurrency: 1,
-    size: Math.pow(poolMaxSize, 2),
+    size: poolMaxSize ** 2,
     tasksFinishedTimeout: 2000,
     tasksStealingOnBackPressure: true,
     tasksStealingRatio: 0.6,
@@ -71,19 +71,23 @@ export const checkDynamicPoolSize = (
     throw new TypeError(
       'Cannot instantiate a dynamic pool without specifying the maximum pool size'
     )
-  } else if (!Number.isSafeInteger(max)) {
+  }
+  if (!Number.isSafeInteger(max)) {
     throw new TypeError(
       'Cannot instantiate a dynamic pool with a non safe integer maximum pool size'
     )
-  } else if (min > max) {
+  }
+  if (min > max) {
     throw new RangeError(
       'Cannot instantiate a dynamic pool with a maximum pool size inferior to the minimum pool size'
     )
-  } else if (max === 0) {
+  }
+  if (max === 0) {
     throw new RangeError(
       'Cannot instantiate a dynamic pool with a maximum pool size equal to zero'
     )
-  } else if (min === max) {
+  }
+  if (min === max) {
     throw new RangeError(
       'Cannot instantiate a dynamic pool with a minimum pool size equal to the maximum pool size. Use a fixed pool instead'
     )
@@ -414,7 +418,8 @@ export const createWorker = <Worker extends IWorker>(
 const getWorkerType = (worker: IWorker): undefined | WorkerType => {
   if (worker instanceof ThreadWorker) {
     return WorkerTypes.thread
-  } else if (worker instanceof ClusterWorker) {
+  }
+  if (worker instanceof ClusterWorker) {
     return WorkerTypes.cluster
   }
 }
@@ -428,7 +433,8 @@ const getWorkerType = (worker: IWorker): undefined | WorkerType => {
 const getWorkerId = (worker: IWorker): number | undefined => {
   if (worker instanceof ThreadWorker) {
     return worker.threadId
-  } else if (worker instanceof ClusterWorker) {
+  }
+  if (worker instanceof ClusterWorker) {
     return worker.id
   }
 }
index 86961b396988ec0cedaa33b55028f745df55bcd2..3b12b82e054f96ece42820bed0923c0d29b119a8 100644 (file)
@@ -188,8 +188,9 @@ export class PriorityQueue<T> {
             // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
             delete tail!.next
             break
-            // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-          } else if (node.next === tail && tail!.next == null) {
+          }
+          // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+          if (node.next === tail && tail!.next == null) {
             delete node.next
             this.head = node
             break
index 3e22b8c19193fe4de39a6d3c795cf0af72134ce1..c7946d8c572fff8c12610a7d005c3c6e1ac2d3a0 100644 (file)
@@ -58,7 +58,7 @@ export const exponentialDelay = (
   retryNumber = 0,
   delayFactor = 100
 ): number => {
-  const delay = Math.pow(2, retryNumber) * delayFactor
+  const delay = 2 ** retryNumber * delayFactor
   const randomSum = delay * 0.2 * secureRandom() // 0-20% of the delay
   return delay + randomSum
 }
@@ -72,7 +72,8 @@ export const exponentialDelay = (
 export const average = (dataSet: number[]): number => {
   if (Array.isArray(dataSet) && dataSet.length === 0) {
     return 0
-  } else if (Array.isArray(dataSet) && dataSet.length === 1) {
+  }
+  if (Array.isArray(dataSet) && dataSet.length === 1) {
     return dataSet[0]
   }
   return (
@@ -90,7 +91,8 @@ export const average = (dataSet: number[]): number => {
 export const median = (dataSet: number[]): number => {
   if (Array.isArray(dataSet) && dataSet.length === 0) {
     return 0
-  } else if (Array.isArray(dataSet) && dataSet.length === 1) {
+  }
+  if (Array.isArray(dataSet) && dataSet.length === 1) {
     return dataSet[0]
   }
   const sortedDataSet = dataSet.slice().sort((a, b) => a - b)
@@ -110,7 +112,7 @@ export const median = (dataSet: number[]): number => {
  * @internal
  */
 export const round = (num: number, scale = 2): number => {
-  const rounder = Math.pow(10, scale)
+  const rounder = 10 ** scale
   return Math.round(num * rounder * (1 + Number.EPSILON)) / rounder
 }
 
index caa95a9b0f64bbf46b6d7f4a1573234a714c9702..0bcc8757db4b3f76baab33bf7668a020d8a8fa29 100644 (file)
@@ -579,7 +579,8 @@ export abstract class AbstractWorker<
   private checkMessageWorkerId (message: MessageValue<Data>): void {
     if (message.workerId == null) {
       throw new Error('Message worker id is not set')
-    } else if (message.workerId !== this.id) {
+    }
+    if (message.workerId !== this.id) {
       throw new Error(
         `Message worker id ${message.workerId.toString()} does not match the worker id ${this.id.toString()}`
       )
index 2887aba774989ee4ab6fbbfa8c5ad6fbc9f9ef3d..452d5b55d52d9991aa89c6aab84e53a2862f0e11 100644 (file)
@@ -275,7 +275,7 @@ describe('Abstract pool test suite', () => {
       startWorkers: true,
       tasksQueueOptions: {
         concurrency: 2,
-        size: Math.pow(numberOfWorkers, 2),
+        size: numberOfWorkers ** 2,
         tasksFinishedTimeout: 2000,
         tasksStealingOnBackPressure: true,
         tasksStealingRatio: 0.6,
@@ -620,7 +620,7 @@ describe('Abstract pool test suite', () => {
     expect(pool.opts.enableTasksQueue).toBe(true)
     expect(pool.opts.tasksQueueOptions).toStrictEqual({
       concurrency: 1,
-      size: Math.pow(numberOfWorkers, 2),
+      size: numberOfWorkers ** 2,
       tasksFinishedTimeout: 2000,
       tasksStealingOnBackPressure: true,
       tasksStealingRatio: 0.6,
@@ -630,7 +630,7 @@ describe('Abstract pool test suite', () => {
     expect(pool.opts.enableTasksQueue).toBe(true)
     expect(pool.opts.tasksQueueOptions).toStrictEqual({
       concurrency: 2,
-      size: Math.pow(numberOfWorkers, 2),
+      size: numberOfWorkers ** 2,
       tasksFinishedTimeout: 2000,
       tasksStealingOnBackPressure: true,
       tasksStealingRatio: 0.6,
@@ -650,7 +650,7 @@ describe('Abstract pool test suite', () => {
     )
     expect(pool.opts.tasksQueueOptions).toStrictEqual({
       concurrency: 1,
-      size: Math.pow(numberOfWorkers, 2),
+      size: numberOfWorkers ** 2,
       tasksFinishedTimeout: 2000,
       tasksStealingOnBackPressure: true,
       tasksStealingRatio: 0.6,
index 5f7f5102ec535ead82e7995f6234e8e147c13369..dc3964160735f331e6a099e0ed62c61f16961060 100644 (file)
@@ -26,7 +26,7 @@ describe('Pool utils test suite', () => {
     const poolMaxSize = 4
     expect(getDefaultTasksQueueOptions(poolMaxSize)).toStrictEqual({
       concurrency: 1,
-      size: Math.pow(poolMaxSize, 2),
+      size: poolMaxSize ** 2,
       tasksFinishedTimeout: 2000,
       tasksStealingOnBackPressure: true,
       tasksStealingRatio: 0.6,
index 10fa7fa0de3f1493fcd50835a8d2387d16caa735..5c6f394d76fd9ee9c16e4ab4f924229de0c02620 100644 (file)
@@ -87,13 +87,12 @@ const fibonacci = n => {
 const factorial = n => {
   if (n === 0 || n === 1) {
     return 1
-  } else {
-    let factorial = 1
-    for (let i = 1; i <= n; i++) {
-      factorial *= i
-    }
-    return factorial
   }
+  let factorial = 1
+  for (let i = 1; i <= n; i++) {
+    factorial *= i
+  }
+  return factorial
 }
 
 const executeTaskFunction = data => {