Use strict compiler flag
[poolifier.git] / src / fixed.ts
1 /* eslint-disable @typescript-eslint/strict-boolean-expressions */
2
3 import { MessageChannel, SHARE_ENV, Worker, isMainThread } from 'worker_threads'
4
5 function empty (): void {}
6 const _void = {}
7
8 export type Draft<T> = { -readonly [P in keyof T]?: T[P] }
9
10 export type WorkerWithMessageChannel = Worker & Draft<MessageChannel>
11
12 export interface FixedThreadPoolOptions {
13 /**
14 * A function that will listen for error event on each worker thread.
15 */
16 errorHandler?: (this: Worker, e: Error) => void
17 /**
18 * A function that will listen for online event on each worker thread.
19 */
20 onlineHandler?: (this: Worker) => void
21 /**
22 * A function that will listen for exit event on each worker thread.
23 */
24 exitHandler?: (this: Worker, code: number) => void
25 /**
26 * This is just to avoid not useful warnings message, is used to set `maxListeners` on event emitters (workers are event emitters).
27 *
28 * @default 1000
29 */
30 maxTasks?: number
31 }
32
33 /**
34 * A thread pool with a static number of threads, is possible to execute tasks in sync or async mode as you prefer.
35 *
36 * This pool will select the worker thread in a round robin fashion.
37 *
38 * @author [Alessandro Pio Ardizio](https://github.com/pioardi)
39 * @since 0.0.1
40 */
41 export default class FixedThreadPool<Data = any, Response = any> {
42 public readonly workers: WorkerWithMessageChannel[] = []
43 public nextWorker: number = 0
44
45 // threadId as key and an integer value
46 /* eslint-disable @typescript-eslint/indent */
47 public readonly tasks: Map<WorkerWithMessageChannel, number> = new Map<
48 WorkerWithMessageChannel,
49 number
50 >()
51 /* eslint-enable @typescript-eslint/indent */
52
53 protected _id: number = 0
54
55 /**
56 * @param numThreads Num of threads for this worker pool.
57 * @param filePath A file path with implementation of `ThreadWorker` class, relative path is fine.
58 * @param opts An object with possible options for example `errorHandler`, `onlineHandler`. Default: `{ maxTasks: 1000 }`
59 */
60 public constructor (
61 public readonly numThreads: number,
62 public readonly filePath: string,
63 public readonly opts: FixedThreadPoolOptions = { maxTasks: 1000 }
64 ) {
65 if (!isMainThread) {
66 throw new Error('Cannot start a thread pool from a worker thread !!!')
67 }
68 // TODO christopher 2021-02-07: Improve this check e.g. with a pattern or blank check
69 if (!this.filePath) {
70 throw new Error('Please specify a file with a worker implementation')
71 }
72
73 for (let i = 1; i <= this.numThreads; i++) {
74 this._newWorker()
75 }
76 }
77
78 public async destroy (): Promise<void> {
79 for (const worker of this.workers) {
80 await worker.terminate()
81 }
82 }
83
84 /**
85 * Execute the task specified into the constructor with the data parameter.
86 *
87 * @param data The input for the task specified.
88 * @returns Promise that is resolved when the task is done.
89 */
90 // eslint-disable-next-line @typescript-eslint/promise-function-async
91 public execute (data: Data): Promise<Response> {
92 // configure worker to handle message with the specified task
93 const worker = this._chooseWorker()
94 this.tasks.set(worker, (this.tasks.get(worker) ?? 0) + 1)
95 const id = ++this._id
96 const res = this._execute(worker, id)
97 worker.postMessage({ data: data || _void, _id: id })
98 return res
99 }
100
101 // eslint-disable-next-line @typescript-eslint/promise-function-async
102 protected _execute (
103 worker: WorkerWithMessageChannel,
104 id: number
105 ): Promise<Response> {
106 return new Promise((resolve, reject) => {
107 const listener = (message: {
108 _id: number
109 error?: string
110 data: Response
111 }): void => {
112 if (message._id === id) {
113 worker.port2?.removeListener('message', listener)
114 this.tasks.set(worker, (this.tasks.get(worker) ?? 0) - 1)
115 if (message.error) reject(message.error)
116 else resolve(message.data)
117 }
118 }
119 worker.port2?.on('message', listener)
120 })
121 }
122
123 protected _chooseWorker (): WorkerWithMessageChannel {
124 if (this.workers.length - 1 === this.nextWorker) {
125 this.nextWorker = 0
126 return this.workers[this.nextWorker]
127 } else {
128 this.nextWorker++
129 return this.workers[this.nextWorker]
130 }
131 }
132
133 protected _newWorker (): WorkerWithMessageChannel {
134 const worker: WorkerWithMessageChannel = new Worker(this.filePath, {
135 env: SHARE_ENV
136 })
137 worker.on('error', this.opts.errorHandler ?? empty)
138 worker.on('online', this.opts.onlineHandler ?? empty)
139 // TODO handle properly when a thread exit
140 worker.on('exit', this.opts.exitHandler ?? empty)
141 this.workers.push(worker)
142 const { port1, port2 } = new MessageChannel()
143 worker.postMessage({ parent: port1 }, [port1])
144 worker.port1 = port1
145 worker.port2 = port2
146 // we will attach a listener for every task,
147 // when task is completed the listener will be removed but to avoid warnings we are increasing the max listeners size
148 worker.port2.setMaxListeners(this.opts.maxTasks ?? 1000)
149 // init tasks map
150 this.tasks.set(worker, 0)
151 return worker
152 }
153 }
154
155 module.exports = FixedThreadPool