X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Futils.ts;h=82c38c09e7161fd06e2c6f1628fdaad1e8f4c63f;hb=bdb9d7125f62a36dd65cba6aa9110ce269359f0a;hp=b5fb9afeda348ddd15e9c441a3c23d67c07ffeb0;hpb=68cbdc846878bc058323b757a68b4c83eedc6388;p=poolifier.git diff --git a/src/utils.ts b/src/utils.ts index b5fb9afe..82c38c09 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,10 +1,18 @@ import * as os from 'node:os' +import { webcrypto } from 'node:crypto' +import { Worker as ClusterWorker } from 'node:cluster' +import { Worker as ThreadWorker } from 'node:worker_threads' import type { MeasurementStatisticsRequirements, WorkerChoiceStrategyOptions } from './pools/selection-strategies/selection-strategies-types' import type { KillBehavior } from './worker/worker-options' -import type { MeasurementStatistics } from './pools/worker' +import { + type IWorker, + type MeasurementStatistics, + type WorkerType, + WorkerTypes +} from './pools/worker' /** * Default task name. @@ -23,7 +31,7 @@ export const EMPTY_FUNCTION: () => void = Object.freeze(() => { */ export const DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS: WorkerChoiceStrategyOptions = { - choiceRetries: 6, + retries: 6, runTime: { median: false }, waitTime: { median: false }, elu: { median: false } @@ -59,6 +67,40 @@ export const availableParallelism = (): number => { return availableParallelism } +/** + * Returns the worker type of the given worker. + * + * @param worker - The worker to get the type of. + * @returns The worker type of the given worker. + * @internal + */ +export const getWorkerType = ( + worker: Worker +): WorkerType | undefined => { + if (worker instanceof ThreadWorker) { + return WorkerTypes.thread + } else if (worker instanceof ClusterWorker) { + return WorkerTypes.cluster + } +} + +/** + * Returns the worker id of the given worker. + * + * @param worker - The worker to get the id of. + * @returns The worker id of the given worker. + * @internal + */ +export const getWorkerId = ( + worker: Worker +): number | undefined => { + if (worker instanceof ThreadWorker) { + return worker.threadId + } else if (worker instanceof ClusterWorker) { + return worker.id + } +} + /** * Sleeps for the given amount of milliseconds. * @@ -75,16 +117,16 @@ export const sleep = async (ms: number): Promise => { * Computes the retry delay in milliseconds using an exponential back off algorithm. * * @param retryNumber - The number of retries that have already been attempted - * @param maxDelayRatio - The maximum ratio of the delay that can be randomized + * @param delayFactor - The base delay factor in milliseconds * @returns Delay in milliseconds * @internal */ export const exponentialDelay = ( retryNumber = 0, - maxDelayRatio = 0.2 + delayFactor = 100 ): number => { - const delay = Math.pow(2, retryNumber) * 100 - const randomSum = delay * maxDelayRatio * secureRandom() // 0-(maxDelayRatio*100)% of the delay + const delay = Math.pow(2, retryNumber) * delayFactor + const randomSum = delay * 0.2 * secureRandom() // 0-20% of the delay return delay + randomSum } @@ -215,43 +257,23 @@ export const updateMeasurementStatistics = ( measurementStatistics.history.push(measurementValue) if (measurementRequirements.average) { measurementStatistics.average = average(measurementStatistics.history) + } else if (measurementStatistics.average != null) { + delete measurementStatistics.average } if (measurementRequirements.median) { measurementStatistics.median = median(measurementStatistics.history) + } else if (measurementStatistics.median != null) { + delete measurementStatistics.median } } } } -/** - * Executes a function once at a time. - * - * @param fn - The function to execute. - * @param context - The context to bind the function to. - * @returns The function to execute. - */ -export const once = ( - // eslint-disable-next-line @typescript-eslint/no-explicit-any - fn: (...args: any[]) => void, - context: unknown - // eslint-disable-next-line @typescript-eslint/no-explicit-any -): ((...args: any[]) => void) => { - let called = false - // eslint-disable-next-line @typescript-eslint/no-explicit-any - return function (...args: any[]): void { - if (!called) { - called = true - fn.apply(context, args) - called = false - } - } -} - /** * Generate a cryptographically secure random number in the [0,1[ range * * @returns A number in the [0,1[ range */ -const secureRandom = (): number => { - return crypto.getRandomValues(new Uint32Array(1))[0] / 0x100000000 +export const secureRandom = (): number => { + return webcrypto.getRandomValues(new Uint32Array(1))[0] / 0x100000000 }