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.
return availableParallelism
}
-// /**
-// * 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
-// * @returns Delay in milliseconds
-// * @internal
-// */
-// export const exponentialDelay = (
-// retryNumber = 0,
-// maxDelayRatio = 0.2
-// ): number => {
-// const delay = Math.pow(2, retryNumber) * 100
-// const randomSum = delay * maxDelayRatio * Math.random() // 0-(maxDelayRatio*100)% of the delay
-// return delay + randomSum
-// }
+/**
+ * Sleeps for the given amount of milliseconds.
+ *
+ * @param ms - The amount of milliseconds to sleep.
+ * @returns A promise that resolves after the given amount of milliseconds.
+ */
+export const sleep = async (ms: number): Promise<void> => {
+ await new Promise((resolve) => {
+ setTimeout(resolve, ms)
+ })
+}
+
+/**
+ * Computes the retry delay in milliseconds using an exponential back off algorithm.
+ *
+ * @param retryNumber - The number of retries that have already been attempted
+ * @param delayFactor - The base delay factor in milliseconds
+ * @returns Delay in milliseconds
+ * @internal
+ */
+export const exponentialDelay = (
+ retryNumber = 0,
+ delayFactor = 100
+): number => {
+ const delay = Math.pow(2, retryNumber) * delayFactor
+ const randomSum = delay * 0.2 * secureRandom() // 0-20% of the delay
+ return delay + randomSum
+}
/**
* Computes the average of the given data set.
)
}
+/**
+ * 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 extends IWorker>(
+ worker: Worker
+): WorkerType | undefined => {
+ if (worker instanceof ThreadWorker) {
+ return WorkerTypes.thread
+ }
+ 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 extends IWorker>(
+ worker: Worker
+): number | undefined => {
+ if (worker instanceof ThreadWorker) {
+ return worker.threadId
+ } else if (worker instanceof ClusterWorker) {
+ return worker.id
+ }
+}
+
/**
* Computes the median of the given data set.
*
}
}
}
+
+/**
+ * Generate a cryptographically secure random number in the [0,1[ range
+ *
+ * @returns A number in the [0,1[ range
+ */
+export const secureRandom = (): number => {
+ return webcrypto.getRandomValues(new Uint32Array(1))[0] / 0x100000000
+}