import { AsyncResource } from 'node:async_hooks'
import type { Worker } from 'node:cluster'
import type { MessagePort } from 'node:worker_threads'
import { AsyncResource } from 'node:async_hooks'
import type { Worker } from 'node:cluster'
import type { MessagePort } from 'node:worker_threads'
} from '../utility-types'
import { EMPTY_FUNCTION, isPlainObject } from '../utils'
import {
} from '../utility-types'
import { EMPTY_FUNCTION, isPlainObject } from '../utils'
import {
const DEFAULT_FUNCTION_NAME = 'default'
const DEFAULT_MAX_INACTIVE_TIME = 60000
const DEFAULT_KILL_BEHAVIOR: KillBehavior = KillBehaviors.SOFT
const DEFAULT_FUNCTION_NAME = 'default'
const DEFAULT_MAX_INACTIVE_TIME = 60000
const DEFAULT_KILL_BEHAVIOR: KillBehavior = KillBehaviors.SOFT
/**
* Base class that implements some shared logic for all poolifier workers.
*
* @typeParam MainWorker - Type of main worker.
/**
* Base class that implements some shared logic for all poolifier workers.
*
* @typeParam MainWorker - Type of main worker.
- * @typeParam Data - Type of data this worker receives from pool's execution. This can only be serializable data.
- * @typeParam Response - Type of response the worker sends back to the main worker. This can only be serializable data.
+ * @typeParam Data - Type of data this worker receives from pool's execution. This can only be structured-cloneable data.
+ * @typeParam Response - Type of response the worker sends back to the main worker. This can only be structured-cloneable data.
*/
export abstract class AbstractWorker<
MainWorker extends Worker | MessagePort,
Data = unknown,
Response = unknown
> extends AsyncResource {
*/
export abstract class AbstractWorker<
MainWorker extends Worker | MessagePort,
Data = unknown,
Response = unknown
> extends AsyncResource {
taskFunctions:
| WorkerFunction<Data, Response>
| TaskFunctions<Data, Response>,
taskFunctions:
| WorkerFunction<Data, Response>
| TaskFunctions<Data, Response>,
protected readonly opts: WorkerOptions = {
/**
* The kill behavior option on this worker or its default value.
protected readonly opts: WorkerOptions = {
/**
* The kill behavior option on this worker or its default value.
} else {
this.runInAsyncScope(this.runSync.bind(this), this, fn, message)
}
} else {
this.runInAsyncScope(this.runSync.bind(this), this, fn, message)
}
- } else if (message.parent != null) {
- // Main worker reference message received
- this.mainWorker = message.parent
+ } else if (message.statistics != null) {
+ // Statistics message received
+ this.statistics = message.statistics
* Handles an error and convert it to a string so it can be sent back to the main worker.
*
* @param e - The error raised by the worker.
* Handles an error and convert it to a string so it can be sent back to the main worker.
*
* @param e - The error raised by the worker.
fn: WorkerAsyncFunction<Data, Response>,
message: MessageValue<Data>
): void {
fn: WorkerAsyncFunction<Data, Response>,
message: MessageValue<Data>
): void {
- timestamp,
- waitTime: timestamp - (message.submissionTimestamp ?? 0),
- elu: performance.eventLoopUtilization()
+ timestamp: performance.now(),
+ ...(this.statistics.elu && { elu: performance.eventLoopUtilization() })
- ...{
- runTime: performance.now() - taskPerformance.timestamp,
+ ...(this.statistics.runTime && {
+ runTime: performance.now() - taskPerformance.timestamp
+ }),
+ ...(this.statistics.elu && {