import { MessageChannel } from 'node:worker_threads'
import { CircularArray } from '../circular-array'
import type { Task } from '../utility-types'
-import { DEFAULT_TASK_NAME, once } from '../utils'
+import {
+ DEFAULT_TASK_NAME,
+ EMPTY_FUNCTION,
+ exponentialDelay,
+ sleep
+} from '../utils'
import { Deque } from '../deque'
import {
type IWorker,
/** @inheritdoc */
public readonly info: WorkerInfo
/** @inheritdoc */
- public messageChannel?: MessageChannel
- /** @inheritdoc */
public usage: WorkerUsage
/** @inheritdoc */
+ public messageChannel?: MessageChannel
+ /** @inheritdoc */
public tasksQueueBackPressureSize: number
/** @inheritdoc */
public onBackPressure?: (workerId: number) => void
/** @inheritdoc */
public onEmptyQueue?: (workerId: number) => void
- private readonly taskFunctionsUsage: Map<string, WorkerUsage>
private readonly tasksQueue: Deque<Task<Data>>
+ private onEmptyQueueCount: number
+ private readonly taskFunctionsUsage: Map<string, WorkerUsage>
/**
* Constructs a new worker node.
}
this.worker = worker
this.info = this.initWorkerInfo(worker, workerType)
+ this.usage = this.initWorkerUsage()
if (workerType === WorkerTypes.thread) {
this.messageChannel = new MessageChannel()
}
- this.usage = this.initWorkerUsage()
- this.taskFunctionsUsage = new Map<string, WorkerUsage>()
- this.tasksQueue = new Deque<Task<Data>>()
this.tasksQueueBackPressureSize = tasksQueueBackPressureSize
+ this.tasksQueue = new Deque<Task<Data>>()
+ this.onEmptyQueueCount = 0
+ this.taskFunctionsUsage = new Map<string, WorkerUsage>()
}
/** @inheritdoc */
public enqueueTask (task: Task<Data>): number {
const tasksQueueSize = this.tasksQueue.push(task)
if (this.onBackPressure != null && this.hasBackPressure()) {
- once(this.onBackPressure, this)(this.info.id as number)
+ this.onBackPressure(this.info.id as number)
}
return tasksQueueSize
}
public unshiftTask (task: Task<Data>): number {
const tasksQueueSize = this.tasksQueue.unshift(task)
if (this.onBackPressure != null && this.hasBackPressure()) {
- once(this.onBackPressure, this)(this.info.id as number)
+ this.onBackPressure(this.info.id as number)
}
return tasksQueueSize
}
public dequeueTask (): Task<Data> | undefined {
const task = this.tasksQueue.shift()
if (this.onEmptyQueue != null && this.tasksQueue.size === 0) {
- once(this.onEmptyQueue, this)(this.info.id as number)
+ this.startOnEmptyQueue().catch(EMPTY_FUNCTION)
}
return task
}
public popTask (): Task<Data> | undefined {
const task = this.tasksQueue.pop()
if (this.onEmptyQueue != null && this.tasksQueue.size === 0) {
- once(this.onEmptyQueue, this)(this.info.id as number)
+ this.startOnEmptyQueue().catch(EMPTY_FUNCTION)
}
return task
}
return this.taskFunctionsUsage.get(name)
}
+ private async startOnEmptyQueue (): Promise<void> {
+ if (
+ this.onEmptyQueueCount > 0 &&
+ (this.usage.tasks.executing > 0 || this.tasksQueue.size > 0)
+ ) {
+ this.onEmptyQueueCount = 0
+ return
+ }
+ (this.onEmptyQueue as (workerId: number) => void)(this.info.id as number)
+ ++this.onEmptyQueueCount
+ await sleep(exponentialDelay(this.onEmptyQueueCount))
+ await this.startOnEmptyQueue()
+ }
+
private initWorkerInfo (worker: Worker, workerType: WorkerType): WorkerInfo {
return {
id: this.getWorkerId(worker, workerType),
get maxQueued (): number {
return getTasksQueueMaxSize()
},
+ stolen: 0,
failed: 0
},
runTime: {
get queued (): number {
return getTaskFunctionQueueSize()
},
+ stolen: 0,
failed: 0
},
runTime: {