Merge dependabot/npm_and_yarn/examples/typescript/websocket-server-pool/ws-cluster...
[poolifier.git] / src / pools / worker-node.ts
1 import { MessageChannel } from 'node:worker_threads'
2 import { CircularArray } from '../circular-array'
3 import type { Task } from '../utility-types'
4 import { DEFAULT_TASK_NAME, once } from '../utils'
5 import { Deque } from '../deque'
6 import {
7 type IWorker,
8 type IWorkerNode,
9 type WorkerInfo,
10 type WorkerType,
11 WorkerTypes,
12 type WorkerUsage
13 } from './worker'
14
15 /**
16 * Worker node.
17 *
18 * @typeParam Worker - Type of worker.
19 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
20 */
21 export class WorkerNode<Worker extends IWorker, Data = unknown>
22 implements IWorkerNode<Worker, Data> {
23 /** @inheritdoc */
24 public readonly worker: Worker
25 /** @inheritdoc */
26 public readonly info: WorkerInfo
27 /** @inheritdoc */
28 public messageChannel?: MessageChannel
29 /** @inheritdoc */
30 public usage: WorkerUsage
31 /** @inheritdoc */
32 public tasksQueueBackPressureSize: number
33 /** @inheritdoc */
34 public onBackPressure?: (workerId: number) => void
35 /** @inheritdoc */
36 public onEmptyQueue?: (workerId: number) => void
37 private readonly taskFunctionsUsage: Map<string, WorkerUsage>
38 private readonly tasksQueue: Deque<Task<Data>>
39
40 /**
41 * Constructs a new worker node.
42 *
43 * @param worker - The worker.
44 * @param workerType - The worker type.
45 * @param tasksQueueBackPressureSize - The tasks queue back pressure size.
46 */
47 constructor (
48 worker: Worker,
49 workerType: WorkerType,
50 tasksQueueBackPressureSize: number
51 ) {
52 if (worker == null) {
53 throw new TypeError('Cannot construct a worker node without a worker')
54 }
55 if (workerType == null) {
56 throw new TypeError(
57 'Cannot construct a worker node without a worker type'
58 )
59 }
60 if (tasksQueueBackPressureSize == null) {
61 throw new TypeError(
62 'Cannot construct a worker node without a tasks queue back pressure size'
63 )
64 }
65 if (!Number.isSafeInteger(tasksQueueBackPressureSize)) {
66 throw new TypeError(
67 'Cannot construct a worker node with a tasks queue back pressure size that is not an integer'
68 )
69 }
70 this.worker = worker
71 this.info = this.initWorkerInfo(worker, workerType)
72 if (workerType === WorkerTypes.thread) {
73 this.messageChannel = new MessageChannel()
74 }
75 this.usage = this.initWorkerUsage()
76 this.taskFunctionsUsage = new Map<string, WorkerUsage>()
77 this.tasksQueue = new Deque<Task<Data>>()
78 this.tasksQueueBackPressureSize = tasksQueueBackPressureSize
79 }
80
81 /** @inheritdoc */
82 public tasksQueueSize (): number {
83 return this.tasksQueue.size
84 }
85
86 /** @inheritdoc */
87 public enqueueTask (task: Task<Data>): number {
88 const tasksQueueSize = this.tasksQueue.push(task)
89 if (this.onBackPressure != null && this.hasBackPressure()) {
90 once(this.onBackPressure, this)(this.info.id as number)
91 }
92 return tasksQueueSize
93 }
94
95 /** @inheritdoc */
96 public unshiftTask (task: Task<Data>): number {
97 const tasksQueueSize = this.tasksQueue.unshift(task)
98 if (this.onBackPressure != null && this.hasBackPressure()) {
99 once(this.onBackPressure, this)(this.info.id as number)
100 }
101 return tasksQueueSize
102 }
103
104 /** @inheritdoc */
105 public dequeueTask (): Task<Data> | undefined {
106 const task = this.tasksQueue.shift()
107 if (this.onEmptyQueue != null && this.tasksQueue.size === 0) {
108 once(this.onEmptyQueue, this)(this.info.id as number)
109 }
110 return task
111 }
112
113 /** @inheritdoc */
114 public popTask (): Task<Data> | undefined {
115 const task = this.tasksQueue.pop()
116 if (this.onEmptyQueue != null && this.tasksQueue.size === 0) {
117 once(this.onEmptyQueue, this)(this.info.id as number)
118 }
119 return task
120 }
121
122 /** @inheritdoc */
123 public clearTasksQueue (): void {
124 this.tasksQueue.clear()
125 }
126
127 /** @inheritdoc */
128 public hasBackPressure (): boolean {
129 return this.tasksQueue.size >= this.tasksQueueBackPressureSize
130 }
131
132 /** @inheritdoc */
133 public resetUsage (): void {
134 this.usage = this.initWorkerUsage()
135 this.taskFunctionsUsage.clear()
136 }
137
138 /** @inheritdoc */
139 public closeChannel (): void {
140 if (this.messageChannel != null) {
141 this.messageChannel?.port1.unref()
142 this.messageChannel?.port2.unref()
143 this.messageChannel?.port1.close()
144 this.messageChannel?.port2.close()
145 delete this.messageChannel
146 }
147 }
148
149 /** @inheritdoc */
150 public getTaskFunctionWorkerUsage (name: string): WorkerUsage | undefined {
151 if (!Array.isArray(this.info.taskFunctions)) {
152 throw new Error(
153 `Cannot get task function worker usage for task function name '${name}' when task function names list is not yet defined`
154 )
155 }
156 if (
157 Array.isArray(this.info.taskFunctions) &&
158 this.info.taskFunctions.length < 3
159 ) {
160 throw new Error(
161 `Cannot get task function worker usage for task function name '${name}' when task function names list has less than 3 elements`
162 )
163 }
164 if (name === DEFAULT_TASK_NAME) {
165 name = this.info.taskFunctions[1]
166 }
167 if (!this.taskFunctionsUsage.has(name)) {
168 this.taskFunctionsUsage.set(name, this.initTaskFunctionWorkerUsage(name))
169 }
170 return this.taskFunctionsUsage.get(name)
171 }
172
173 private initWorkerInfo (worker: Worker, workerType: WorkerType): WorkerInfo {
174 return {
175 id: this.getWorkerId(worker, workerType),
176 type: workerType,
177 dynamic: false,
178 ready: false
179 }
180 }
181
182 private initWorkerUsage (): WorkerUsage {
183 const getTasksQueueSize = (): number => {
184 return this.tasksQueue.size
185 }
186 const getTasksQueueMaxSize = (): number => {
187 return this.tasksQueue.maxSize
188 }
189 return {
190 tasks: {
191 executed: 0,
192 executing: 0,
193 get queued (): number {
194 return getTasksQueueSize()
195 },
196 get maxQueued (): number {
197 return getTasksQueueMaxSize()
198 },
199 failed: 0
200 },
201 runTime: {
202 history: new CircularArray()
203 },
204 waitTime: {
205 history: new CircularArray()
206 },
207 elu: {
208 idle: {
209 history: new CircularArray()
210 },
211 active: {
212 history: new CircularArray()
213 }
214 }
215 }
216 }
217
218 private initTaskFunctionWorkerUsage (name: string): WorkerUsage {
219 const getTaskFunctionQueueSize = (): number => {
220 let taskFunctionQueueSize = 0
221 for (const task of this.tasksQueue) {
222 if (
223 (task.name === DEFAULT_TASK_NAME &&
224 name === (this.info.taskFunctions as string[])[1]) ||
225 (task.name !== DEFAULT_TASK_NAME && name === task.name)
226 ) {
227 ++taskFunctionQueueSize
228 }
229 }
230 return taskFunctionQueueSize
231 }
232 return {
233 tasks: {
234 executed: 0,
235 executing: 0,
236 get queued (): number {
237 return getTaskFunctionQueueSize()
238 },
239 failed: 0
240 },
241 runTime: {
242 history: new CircularArray()
243 },
244 waitTime: {
245 history: new CircularArray()
246 },
247 elu: {
248 idle: {
249 history: new CircularArray()
250 },
251 active: {
252 history: new CircularArray()
253 }
254 }
255 }
256 }
257
258 /**
259 * Gets the worker id.
260 *
261 * @param worker - The worker.
262 * @param workerType - The worker type.
263 * @returns The worker id.
264 */
265 private getWorkerId (
266 worker: Worker,
267 workerType: WorkerType
268 ): number | undefined {
269 if (workerType === WorkerTypes.thread) {
270 return worker.threadId
271 } else if (workerType === WorkerTypes.cluster) {
272 return worker.id
273 }
274 }
275 }