1 import { MessageChannel
} from
'node:worker_threads'
2 import { CircularArray
} from
'../circular-array'
3 import { Queue
} from
'../queue'
4 import type { Task
} from
'../utility-types'
5 import { DEFAULT_TASK_NAME
} from
'../utils'
18 * @typeParam Worker - Type of worker.
19 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
21 export class WorkerNode
<Worker
extends IWorker
, Data
= unknown
>
22 implements IWorkerNode
<Worker
, Data
> {
24 public readonly worker
: Worker
26 public readonly info
: WorkerInfo
28 public messageChannel
?: MessageChannel
30 public usage
: WorkerUsage
31 private readonly taskFunctionsUsage
: Map
<string, WorkerUsage
>
32 private readonly tasksQueue
: Queue
<Task
<Data
>>
33 private readonly tasksQueueBackPressureSize
: number
36 * Constructs a new worker node.
38 * @param worker - The worker.
39 * @param workerType - The worker type.
40 * @param poolMaxSize - The pool maximum size.
42 constructor (worker
: Worker
, workerType
: WorkerType
, poolMaxSize
: number) {
44 this.info
= this.initWorkerInfo(worker
, workerType
)
45 if (workerType
=== WorkerTypes
.thread
) {
46 this.messageChannel
= new MessageChannel()
48 this.usage
= this.initWorkerUsage()
49 this.taskFunctionsUsage
= new Map
<string, WorkerUsage
>()
50 this.tasksQueue
= new Queue
<Task
<Data
>>()
51 this.tasksQueueBackPressureSize
= Math.pow(poolMaxSize
, 2)
55 public tasksQueueSize (): number {
56 return this.tasksQueue
.size
60 * Tasks queue maximum size.
62 * @returns The tasks queue maximum size.
64 private tasksQueueMaxSize (): number {
65 return this.tasksQueue
.maxSize
69 public enqueueTask (task
: Task
<Data
>): number {
70 return this.tasksQueue
.enqueue(task
)
74 public dequeueTask (): Task
<Data
> | undefined {
75 return this.tasksQueue
.dequeue()
79 public clearTasksQueue (): void {
80 this.tasksQueue
.clear()
84 public hasBackPressure (): boolean {
85 return this.tasksQueueSize() >= this.tasksQueueBackPressureSize
89 public resetUsage (): void {
90 this.usage
= this.initWorkerUsage()
91 this.taskFunctionsUsage
.clear()
95 public closeChannel (): void {
96 if (this.messageChannel
!= null) {
97 this.messageChannel
?.port1
.unref()
98 this.messageChannel
?.port2
.unref()
99 this.messageChannel
?.port1
.close()
100 this.messageChannel
?.port2
.close()
101 delete this.messageChannel
106 public getTaskFunctionWorkerUsage (name
: string): WorkerUsage
| undefined {
107 if (!Array.isArray(this.info
.taskFunctions
)) {
109 `Cannot get task function worker usage for task function name '${name}' when task function names list is not yet defined`
113 Array.isArray(this.info
.taskFunctions
) &&
114 this.info
.taskFunctions
.length
< 3
117 `Cannot get task function worker usage for task function name '${name}' when task function names list has less than 3 elements`
120 if (name
=== DEFAULT_TASK_NAME
) {
121 name
= this.info
.taskFunctions
[1]
123 if (!this.taskFunctionsUsage
.has(name
)) {
124 this.taskFunctionsUsage
.set(name
, this.initTaskFunctionWorkerUsage(name
))
126 return this.taskFunctionsUsage
.get(name
)
129 private initWorkerInfo (worker
: Worker
, workerType
: WorkerType
): WorkerInfo
{
131 id
: this.getWorkerId(worker
, workerType
),
138 private initWorkerUsage (): WorkerUsage
{
139 const getTasksQueueSize
= (): number => {
140 return this.tasksQueueSize()
142 const getTasksQueueMaxSize
= (): number => {
143 return this.tasksQueueMaxSize()
149 get
queued (): number {
150 return getTasksQueueSize()
152 get
maxQueued (): number {
153 return getTasksQueueMaxSize()
158 history
: new CircularArray()
161 history
: new CircularArray()
165 history
: new CircularArray()
168 history
: new CircularArray()
174 private initTaskFunctionWorkerUsage (name
: string): WorkerUsage
{
175 const getTaskQueueSize
= (): number => {
176 let taskQueueSize
= 0
177 for (const task
of this.tasksQueue
) {
178 if (task
.name
=== name
) {
188 get
queued (): number {
189 return getTaskQueueSize()
194 history
: new CircularArray()
197 history
: new CircularArray()
201 history
: new CircularArray()
204 history
: new CircularArray()
211 * Gets the worker id.
213 * @param worker - The worker.
214 * @param workerType - The worker type.
215 * @returns The worker id.
217 private getWorkerId (
219 workerType
: WorkerType
220 ): number | undefined {
221 if (workerType
=== WorkerTypes
.thread
) {
222 return worker
.threadId
223 } else if (workerType
=== WorkerTypes
.cluster
) {