From: Jérôme Benoit Date: Tue, 21 May 2024 18:20:43 +0000 (+0200) Subject: refactor: code cleanups X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=80115618ce24038f504dc447dfb7c4fbd9c5d698;p=poolifier.git refactor: code cleanups Signed-off-by: Jérôme Benoit --- diff --git a/benchmarks/worker-selection/least.mjs b/benchmarks/worker-selection/least.mjs index ca5de3c1..4be3826f 100644 --- a/benchmarks/worker-selection/least.mjs +++ b/benchmarks/worker-selection/least.mjs @@ -18,7 +18,7 @@ const tasksMap = generateRandomTasksMap(60, 20) function loopSelect (tasksMap) { let minKey - let minValue = Infinity + let minValue = Number.POSITIVE_INFINITY for (const [key, value] of tasksMap) { if (value === 0) { return key diff --git a/examples/typescript/http-client-pool/src/types.ts b/examples/typescript/http-client-pool/src/types.ts index 6db45d5f..9b35949c 100644 --- a/examples/typescript/http-client-pool/src/types.ts +++ b/examples/typescript/http-client-pool/src/types.ts @@ -1,9 +1,9 @@ import type { URL } from 'node:url' import type { AxiosRequestConfig } from 'axios' -import { - type RequestInfo as NodeFetchRequestInfo, - type RequestInit as NodeFetchRequestInit +import type { + RequestInfo as NodeFetchRequestInfo, + RequestInit as NodeFetchRequestInit } from 'node-fetch' export interface WorkerData { diff --git a/examples/typescript/http-server-pool/express-cluster/src/worker.ts b/examples/typescript/http-server-pool/express-cluster/src/worker.ts index 98ca45de..4659e00a 100644 --- a/examples/typescript/http-server-pool/express-cluster/src/worker.ts +++ b/examples/typescript/http-server-pool/express-cluster/src/worker.ts @@ -39,7 +39,9 @@ class ExpressWorker extends ClusterWorker { application.get('/api/factorial/:number', (req: Request, res: Response) => { const { number } = req.params res - .send({ number: ExpressWorker.factorial(parseInt(number)).toString() }) + .send({ + number: ExpressWorker.factorial(Number.parseInt(number)).toString() + }) .end() }) diff --git a/examples/typescript/http-server-pool/express-hybrid/src/express-worker.ts b/examples/typescript/http-server-pool/express-hybrid/src/express-worker.ts index 7ba9f374..bde3ad4e 100644 --- a/examples/typescript/http-server-pool/express-hybrid/src/express-worker.ts +++ b/examples/typescript/http-server-pool/express-hybrid/src/express-worker.ts @@ -8,12 +8,12 @@ import { DynamicThreadPool } from 'poolifier' -import { - type ClusterWorkerData, - type ClusterWorkerResponse, - type DataPayload, - type ThreadWorkerData, - type ThreadWorkerResponse +import type { + ClusterWorkerData, + ClusterWorkerResponse, + DataPayload, + ThreadWorkerData, + ThreadWorkerResponse } from './types.js' const emptyFunction = (): void => { @@ -63,7 +63,7 @@ ClusterWorkerResponse application.get('/api/factorial/:number', (req: Request, res: Response) => { const { number } = req.params ExpressWorker.requestHandlerPool - .execute({ data: { number: parseInt(number) } }, 'factorial') + .execute({ data: { number: Number.parseInt(number) } }, 'factorial') .then(response => { return res.send(response.data).end() }) diff --git a/examples/typescript/http-server-pool/express-hybrid/src/request-handler-worker.ts b/examples/typescript/http-server-pool/express-hybrid/src/request-handler-worker.ts index b7ff8b16..5dbc1137 100644 --- a/examples/typescript/http-server-pool/express-hybrid/src/request-handler-worker.ts +++ b/examples/typescript/http-server-pool/express-hybrid/src/request-handler-worker.ts @@ -1,9 +1,9 @@ import { ThreadWorker } from 'poolifier' -import { - type DataPayload, - type ThreadWorkerData, - type ThreadWorkerResponse +import type { + DataPayload, + ThreadWorkerData, + ThreadWorkerResponse } from './types.js' class RequestHandlerWorker< diff --git a/examples/typescript/http-server-pool/express-worker_threads/src/main.ts b/examples/typescript/http-server-pool/express-worker_threads/src/main.ts index fc0f2d84..e0cb7042 100644 --- a/examples/typescript/http-server-pool/express-worker_threads/src/main.ts +++ b/examples/typescript/http-server-pool/express-worker_threads/src/main.ts @@ -33,7 +33,7 @@ expressApp.all('/api/echo', (req: Request, res: Response) => { expressApp.get('/api/factorial/:number', (req: Request, res: Response) => { const { number } = req.params requestHandlerPool - .execute({ body: { number: parseInt(number) } }, 'factorial') + .execute({ body: { number: Number.parseInt(number) } }, 'factorial') .then(response => { return res.send(response.body).end() }) diff --git a/examples/typescript/http-server-pool/express-worker_threads/src/pool.ts b/examples/typescript/http-server-pool/express-worker_threads/src/pool.ts index 98ae657d..0f6a7acc 100644 --- a/examples/typescript/http-server-pool/express-worker_threads/src/pool.ts +++ b/examples/typescript/http-server-pool/express-worker_threads/src/pool.ts @@ -3,11 +3,7 @@ import { fileURLToPath } from 'node:url' import { availableParallelism, DynamicThreadPool } from 'poolifier' -import { - type BodyPayload, - type WorkerData, - type WorkerResponse -} from './types.js' +import type { BodyPayload, WorkerData, WorkerResponse } from './types.js' const workerFile = join( dirname(fileURLToPath(import.meta.url)), diff --git a/examples/typescript/http-server-pool/express-worker_threads/src/worker.ts b/examples/typescript/http-server-pool/express-worker_threads/src/worker.ts index 18e5c8a3..09810907 100644 --- a/examples/typescript/http-server-pool/express-worker_threads/src/worker.ts +++ b/examples/typescript/http-server-pool/express-worker_threads/src/worker.ts @@ -1,10 +1,6 @@ import { ThreadWorker } from 'poolifier' -import { - type BodyPayload, - type WorkerData, - type WorkerResponse -} from './types.js' +import type { BodyPayload, WorkerData, WorkerResponse } from './types.js' class RequestHandlerWorker< Data extends WorkerData, diff --git a/examples/typescript/http-server-pool/fastify-hybrid/@types/fastify/index.d.ts b/examples/typescript/http-server-pool/fastify-hybrid/@types/fastify/index.d.ts index 018cac59..166bfd33 100644 --- a/examples/typescript/http-server-pool/fastify-hybrid/@types/fastify/index.d.ts +++ b/examples/typescript/http-server-pool/fastify-hybrid/@types/fastify/index.d.ts @@ -3,10 +3,7 @@ import type { TransferListItem } from 'node:worker_threads' import type * as fastify from 'fastify' import type { DynamicThreadPool } from 'poolifier' -import { - type ThreadWorkerData, - type ThreadWorkerResponse -} from '../../src/types.ts' +import type { ThreadWorkerData, ThreadWorkerResponse } from '../../src/types.ts' declare module 'fastify' { export interface FastifyInstance extends fastify.FastifyInstance { diff --git a/examples/typescript/http-server-pool/fastify-hybrid/src/fastify-poolifier.ts b/examples/typescript/http-server-pool/fastify-hybrid/src/fastify-poolifier.ts index f105d7af..b4369082 100644 --- a/examples/typescript/http-server-pool/fastify-hybrid/src/fastify-poolifier.ts +++ b/examples/typescript/http-server-pool/fastify-hybrid/src/fastify-poolifier.ts @@ -4,10 +4,10 @@ import type { FastifyPluginCallback } from 'fastify' import fp from 'fastify-plugin' import { availableParallelism, DynamicThreadPool } from 'poolifier' -import { - type FastifyPoolifierOptions, - type ThreadWorkerData, - type ThreadWorkerResponse +import type { + FastifyPoolifierOptions, + ThreadWorkerData, + ThreadWorkerResponse } from './types.js' const fastifyPoolifierPlugin: FastifyPluginCallback = ( diff --git a/examples/typescript/http-server-pool/fastify-hybrid/src/request-handler-worker.ts b/examples/typescript/http-server-pool/fastify-hybrid/src/request-handler-worker.ts index b7ff8b16..5dbc1137 100644 --- a/examples/typescript/http-server-pool/fastify-hybrid/src/request-handler-worker.ts +++ b/examples/typescript/http-server-pool/fastify-hybrid/src/request-handler-worker.ts @@ -1,9 +1,9 @@ import { ThreadWorker } from 'poolifier' -import { - type DataPayload, - type ThreadWorkerData, - type ThreadWorkerResponse +import type { + DataPayload, + ThreadWorkerData, + ThreadWorkerResponse } from './types.js' class RequestHandlerWorker< diff --git a/examples/typescript/http-server-pool/fastify-worker_threads/src/fastify-poolifier.ts b/examples/typescript/http-server-pool/fastify-worker_threads/src/fastify-poolifier.ts index 450a2aea..c031d287 100644 --- a/examples/typescript/http-server-pool/fastify-worker_threads/src/fastify-poolifier.ts +++ b/examples/typescript/http-server-pool/fastify-worker_threads/src/fastify-poolifier.ts @@ -4,10 +4,10 @@ import type { FastifyPluginCallback } from 'fastify' import fp from 'fastify-plugin' import { availableParallelism, DynamicThreadPool } from 'poolifier' -import { - type FastifyPoolifierOptions, - type WorkerData, - type WorkerResponse +import type { + FastifyPoolifierOptions, + WorkerData, + WorkerResponse } from './types.js' const fastifyPoolifierPlugin: FastifyPluginCallback = ( diff --git a/examples/typescript/http-server-pool/fastify-worker_threads/src/worker.ts b/examples/typescript/http-server-pool/fastify-worker_threads/src/worker.ts index 18e5c8a3..09810907 100644 --- a/examples/typescript/http-server-pool/fastify-worker_threads/src/worker.ts +++ b/examples/typescript/http-server-pool/fastify-worker_threads/src/worker.ts @@ -1,10 +1,6 @@ import { ThreadWorker } from 'poolifier' -import { - type BodyPayload, - type WorkerData, - type WorkerResponse -} from './types.js' +import type { BodyPayload, WorkerData, WorkerResponse } from './types.js' class RequestHandlerWorker< Data extends WorkerData, diff --git a/examples/typescript/websocket-server-pool/ws-hybrid/src/request-handler-worker.ts b/examples/typescript/websocket-server-pool/ws-hybrid/src/request-handler-worker.ts index 004dd3db..99d30331 100644 --- a/examples/typescript/websocket-server-pool/ws-hybrid/src/request-handler-worker.ts +++ b/examples/typescript/websocket-server-pool/ws-hybrid/src/request-handler-worker.ts @@ -1,9 +1,9 @@ import { ThreadWorker } from 'poolifier' -import { - type DataPayload, - type ThreadWorkerData, - type ThreadWorkerResponse +import type { + DataPayload, + ThreadWorkerData, + ThreadWorkerResponse } from './types.js' class RequestHandlerWorker< diff --git a/examples/typescript/websocket-server-pool/ws-worker_threads/src/pool.ts b/examples/typescript/websocket-server-pool/ws-worker_threads/src/pool.ts index 45d458d5..ac8b9c86 100644 --- a/examples/typescript/websocket-server-pool/ws-worker_threads/src/pool.ts +++ b/examples/typescript/websocket-server-pool/ws-worker_threads/src/pool.ts @@ -3,11 +3,7 @@ import { fileURLToPath } from 'node:url' import { availableParallelism, DynamicThreadPool } from 'poolifier' -import { - type DataPayload, - type WorkerData, - type WorkerResponse -} from './types.js' +import type { DataPayload, WorkerData, WorkerResponse } from './types.js' const workerFile = join( dirname(fileURLToPath(import.meta.url)), diff --git a/examples/typescript/websocket-server-pool/ws-worker_threads/src/worker.ts b/examples/typescript/websocket-server-pool/ws-worker_threads/src/worker.ts index e1b67193..92baa166 100644 --- a/examples/typescript/websocket-server-pool/ws-worker_threads/src/worker.ts +++ b/examples/typescript/websocket-server-pool/ws-worker_threads/src/worker.ts @@ -1,10 +1,6 @@ import { ThreadWorker } from 'poolifier' -import { - type DataPayload, - type WorkerData, - type WorkerResponse -} from './types.js' +import type { DataPayload, WorkerData, WorkerResponse } from './types.js' class RequestHandlerWorker< Data extends WorkerData, diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 95d3970f..a23c7983 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -376,14 +376,16 @@ export abstract class AbstractPool< minimum: round( min( ...this.workerNodes.map( - workerNode => workerNode.usage.runTime.minimum ?? Infinity + workerNode => + workerNode.usage.runTime.minimum ?? Number.POSITIVE_INFINITY ) ) ), maximum: round( max( ...this.workerNodes.map( - workerNode => workerNode.usage.runTime.maximum ?? -Infinity + workerNode => + workerNode.usage.runTime.maximum ?? Number.NEGATIVE_INFINITY ) ) ), @@ -419,14 +421,16 @@ export abstract class AbstractPool< minimum: round( min( ...this.workerNodes.map( - workerNode => workerNode.usage.waitTime.minimum ?? Infinity + workerNode => + workerNode.usage.waitTime.minimum ?? Number.POSITIVE_INFINITY ) ) ), maximum: round( max( ...this.workerNodes.map( - workerNode => workerNode.usage.waitTime.maximum ?? -Infinity + workerNode => + workerNode.usage.waitTime.maximum ?? Number.NEGATIVE_INFINITY ) ) ), @@ -463,14 +467,18 @@ export abstract class AbstractPool< minimum: round( min( ...this.workerNodes.map( - workerNode => workerNode.usage.elu.idle.minimum ?? Infinity + workerNode => + workerNode.usage.elu.idle.minimum ?? + Number.POSITIVE_INFINITY ) ) ), maximum: round( max( ...this.workerNodes.map( - workerNode => workerNode.usage.elu.idle.maximum ?? -Infinity + workerNode => + workerNode.usage.elu.idle.maximum ?? + Number.NEGATIVE_INFINITY ) ) ), @@ -503,14 +511,18 @@ export abstract class AbstractPool< minimum: round( min( ...this.workerNodes.map( - workerNode => workerNode.usage.elu.active.minimum ?? Infinity + workerNode => + workerNode.usage.elu.active.minimum ?? + Number.POSITIVE_INFINITY ) ) ), maximum: round( max( ...this.workerNodes.map( - workerNode => workerNode.usage.elu.active.maximum ?? -Infinity + workerNode => + workerNode.usage.elu.active.maximum ?? + Number.NEGATIVE_INFINITY ) ) ), @@ -1495,7 +1507,8 @@ export abstract class AbstractPool< ) { workerNode.usage.runTime.aggregate = min( ...this.workerNodes.map( - workerNode => workerNode.usage.runTime.aggregate ?? Infinity + workerNode => + workerNode.usage.runTime.aggregate ?? Number.POSITIVE_INFINITY ) ) } @@ -1505,7 +1518,8 @@ export abstract class AbstractPool< ) { workerNode.usage.waitTime.aggregate = min( ...this.workerNodes.map( - workerNode => workerNode.usage.waitTime.aggregate ?? Infinity + workerNode => + workerNode.usage.waitTime.aggregate ?? Number.POSITIVE_INFINITY ) ) } @@ -1515,7 +1529,8 @@ export abstract class AbstractPool< ) { workerNode.usage.elu.active.aggregate = min( ...this.workerNodes.map( - workerNode => workerNode.usage.elu.active.aggregate ?? Infinity + workerNode => + workerNode.usage.elu.active.aggregate ?? Number.POSITIVE_INFINITY ) ) } 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 357b770a..d1a4919d 100644 --- a/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts @@ -136,7 +136,7 @@ export class FairShareWorkerChoiceStrategy< this.pool.workerNodes[workerNodeKey]?.strategyData ?.virtualTaskEndTimestamp const now = performance.now() - return now < (virtualTaskEndTimestamp ?? -Infinity) + return now < (virtualTaskEndTimestamp ?? Number.NEGATIVE_INFINITY) ? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion virtualTaskEndTimestamp! : now diff --git a/src/pools/utils.ts b/src/pools/utils.ts index c3ca698c..e69725df 100644 --- a/src/pools/utils.ts +++ b/src/pools/utils.ts @@ -226,11 +226,11 @@ const updateMeasurementStatistics = ( (measurementStatistics.aggregate ?? 0) + measurementValue measurementStatistics.minimum = min( measurementValue, - measurementStatistics.minimum ?? Infinity + measurementStatistics.minimum ?? Number.POSITIVE_INFINITY ) measurementStatistics.maximum = max( measurementValue, - measurementStatistics.maximum ?? -Infinity + measurementStatistics.maximum ?? Number.NEGATIVE_INFINITY ) if (measurementRequirements.average || measurementRequirements.median) { measurementStatistics.history.push(measurementValue) diff --git a/src/priority-queue.ts b/src/priority-queue.ts index 8e9c1ae2..b4d70b5e 100644 --- a/src/priority-queue.ts +++ b/src/priority-queue.ts @@ -30,7 +30,7 @@ export class PriorityQueue { * The number of filled prioritized buckets. */ public get buckets (): number { - return this.bucketSize === Infinity + return this.bucketSize === Number.POSITIVE_INFINITY ? 1 : Math.trunc(this.nodeArray.length / this.bucketSize) } @@ -38,10 +38,13 @@ export class PriorityQueue { /** * Constructs a priority queue. * - * @param bucketSize - Prioritized bucket size. @defaultValue Infinity + * @param bucketSize - Prioritized bucket size. @defaultValue Number.POSITIVE_INFINITY */ - public constructor (bucketSize = Infinity) { - if (bucketSize !== Infinity && !Number.isSafeInteger(bucketSize)) { + public constructor (bucketSize = Number.POSITIVE_INFINITY) { + if ( + bucketSize !== Number.POSITIVE_INFINITY && + !Number.isSafeInteger(bucketSize) + ) { throw new TypeError('bucketSize must be an integer') } if (bucketSize < 1) { @@ -61,7 +64,9 @@ export class PriorityQueue { public enqueue (data: T, priority?: number): number { priority = priority ?? 0 const startIndex = - this.bucketSize === Infinity ? 0 : this.buckets * this.bucketSize + this.bucketSize === Number.POSITIVE_INFINITY + ? 0 + : this.buckets * this.bucketSize let inserted = false for (let index = startIndex; index < this.nodeArray.length; index++) { if (this.nodeArray[index].priority > priority) { @@ -83,7 +88,7 @@ export class PriorityQueue { * @returns The dequeued data or `undefined` if the priority queue is empty. */ public dequeue (bucket = 0): T | undefined { - if (this.bucketSize !== Infinity && bucket > 0) { + if (this.bucketSize !== Number.POSITIVE_INFINITY && bucket > 0) { while (bucket > 0) { const index = bucket * this.bucketSize // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition diff --git a/src/utils.ts b/src/utils.ts index dd839bc4..f3822169 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -174,25 +174,31 @@ export const secureRandom = (): number => { /** * Returns the minimum of the given numbers. - * If no numbers are given, `Infinity` is returned. + * If no numbers are given, `Number.POSITIVE_INFINITY` is returned. * * @param args - The numbers to get the minimum of. * @returns The minimum of the given numbers. * @internal */ export const min = (...args: number[]): number => - args.reduce((minimum, num) => (minimum < num ? minimum : num), Infinity) + args.reduce( + (minimum, num) => (minimum < num ? minimum : num), + Number.POSITIVE_INFINITY + ) /** * Returns the maximum of the given numbers. - * If no numbers are given, `-Infinity` is returned. + * If no numbers are given, `Number.NEGATIVE_INFINITY` is returned. * * @param args - The numbers to get the maximum of. * @returns The maximum of the given numbers. * @internal */ export const max = (...args: number[]): number => - args.reduce((maximum, num) => (maximum > num ? maximum : num), -Infinity) + args.reduce( + (maximum, num) => (maximum > num ? maximum : num), + Number.NEGATIVE_INFINITY + ) /** * Wraps a function so that it can only be called once. diff --git a/tests/priority-queue.test.mjs b/tests/priority-queue.test.mjs index f3da8734..00ecb33f 100644 --- a/tests/priority-queue.test.mjs +++ b/tests/priority-queue.test.mjs @@ -14,7 +14,7 @@ describe('Priority queue test suite', () => { new RangeError('bucketSize must be greater than or equal to 1') ) let priorityQueue = new PriorityQueue() - expect(priorityQueue.bucketSize).toBe(Infinity) + expect(priorityQueue.bucketSize).toBe(Number.POSITIVE_INFINITY) expect(priorityQueue.buckets).toBe(1) expect(priorityQueue.size).toBe(0) expect(priorityQueue.maxSize).toBe(0) diff --git a/tests/utils.test.mjs b/tests/utils.test.mjs index b3c79d9c..6e97797e 100644 --- a/tests/utils.test.mjs +++ b/tests/utils.test.mjs @@ -151,7 +151,7 @@ describe('Utils test suite', () => { expect(isAsyncFunction([])).toBe(false) expect(isAsyncFunction(new Date())).toBe(false) // eslint-disable-next-line prefer-regex-literals - expect(isAsyncFunction(new RegExp('[a-z]', 'i'))).toBe(false) + expect(isAsyncFunction(/[a-z]/i)).toBe(false) expect(isAsyncFunction(new Error())).toBe(false) expect(isAsyncFunction(new Map())).toBe(false) expect(isAsyncFunction(new Set())).toBe(false) @@ -207,14 +207,14 @@ describe('Utils test suite', () => { }) it('Verify min() behavior', () => { - expect(min()).toBe(Infinity) + expect(min()).toBe(Number.POSITIVE_INFINITY) expect(min(1, 2)).toBe(1) expect(min(2, 1)).toBe(1) expect(min(1, 1)).toBe(1) }) it('Verify max() behavior', () => { - expect(max()).toBe(-Infinity) + expect(max()).toBe(Number.NEGATIVE_INFINITY) expect(max(1, 2)).toBe(2) expect(max(2, 1)).toBe(2) expect(max(1, 1)).toBe(1)