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 throw new Error('Cannot construct a worker node without a worker')
46 if (workerType
== null) {
47 throw new Error('Cannot construct a worker node without a worker type')
49 if (poolMaxSize
== null) {
51 'Cannot construct a worker node without a pool maximum size'
54 if (isNaN(poolMaxSize
)) {
56 'Cannot construct a worker node with a NaN pool maximum size'
60 this.info
= this.initWorkerInfo(worker
, workerType
)
61 if (workerType
=== WorkerTypes
.thread
) {
62 this.messageChannel
= new MessageChannel()
64 this.usage
= this.initWorkerUsage()
65 this.taskFunctionsUsage
= new Map
<string, WorkerUsage
>()
66 this.tasksQueue
= new Queue
<Task
<Data
>>()
67 this.tasksQueueBackPressureSize
= Math.pow(poolMaxSize
, 2)
71 public tasksQueueSize (): number {
72 return this.tasksQueue
.size
76 * Tasks queue maximum size.
78 * @returns The tasks queue maximum size.
80 private tasksQueueMaxSize (): number {
81 return this.tasksQueue
.maxSize
85 public enqueueTask (task
: Task
<Data
>): number {
86 return this.tasksQueue
.enqueue(task
)
90 public dequeueTask (): Task
<Data
> | undefined {
91 return this.tasksQueue
.dequeue()
95 public clearTasksQueue (): void {
96 this.tasksQueue
.clear()
100 public hasBackPressure (): boolean {
101 return this.tasksQueue
.size
>= this.tasksQueueBackPressureSize
105 public resetUsage (): void {
106 this.usage
= this.initWorkerUsage()
107 this.taskFunctionsUsage
.clear()
111 public closeChannel (): void {
112 if (this.messageChannel
!= null) {
113 this.messageChannel
?.port1
.unref()
114 this.messageChannel
?.port2
.unref()
115 this.messageChannel
?.port1
.close()
116 this.messageChannel
?.port2
.close()
117 delete this.messageChannel
122 public getTaskFunctionWorkerUsage (name
: string): WorkerUsage
| undefined {
123 if (!Array.isArray(this.info
.taskFunctions
)) {
125 `Cannot get task function worker usage for task function name '${name}' when task function names list is not yet defined`
129 Array.isArray(this.info
.taskFunctions
) &&
130 this.info
.taskFunctions
.length
< 3
133 `Cannot get task function worker usage for task function name '${name}' when task function names list has less than 3 elements`
136 if (name
=== DEFAULT_TASK_NAME
) {
137 name
= this.info
.taskFunctions
[1]
139 if (!this.taskFunctionsUsage
.has(name
)) {
140 this.taskFunctionsUsage
.set(name
, this.initTaskFunctionWorkerUsage(name
))
142 return this.taskFunctionsUsage
.get(name
)
145 private initWorkerInfo (worker
: Worker
, workerType
: WorkerType
): WorkerInfo
{
147 id
: this.getWorkerId(worker
, workerType
),
154 private initWorkerUsage (): WorkerUsage
{
155 const getTasksQueueSize
= (): number => {
156 return this.tasksQueueSize()
158 const getTasksQueueMaxSize
= (): number => {
159 return this.tasksQueueMaxSize()
165 get
queued (): number {
166 return getTasksQueueSize()
168 get
maxQueued (): number {
169 return getTasksQueueMaxSize()
174 history
: new CircularArray()
177 history
: new CircularArray()
181 history
: new CircularArray()
184 history
: new CircularArray()
190 private initTaskFunctionWorkerUsage (name
: string): WorkerUsage
{
191 const getTaskQueueSize
= (): number => {
192 let taskQueueSize
= 0
193 for (const task
of this.tasksQueue
) {
194 if (task
.name
=== name
) {
204 get
queued (): number {
205 return getTaskQueueSize()
210 history
: new CircularArray()
213 history
: new CircularArray()
217 history
: new CircularArray()
220 history
: new CircularArray()
227 * Gets the worker id.
229 * @param worker - The worker.
230 * @param workerType - The worker type.
231 * @returns The worker id.
233 private getWorkerId (
235 workerType
: WorkerType
236 ): number | undefined {
237 if (workerType
=== WorkerTypes
.thread
) {
238 return worker
.threadId
239 } else if (workerType
=== WorkerTypes
.cluster
) {