Cleanups (#144)
[poolifier.git] / src / pools / thread / fixed.ts
CommitLineData
fa699c42 1import { isMainThread, MessageChannel, SHARE_ENV, Worker } from 'worker_threads'
d3c8a1a8 2import type { Draft, JSONValue, MessageValue } from '../../utility-types'
c97c7edb
S
3import type { PoolOptions } from '../abstract-pool'
4import { AbstractPool } from '../abstract-pool'
4ade5f1f 5
729c563d
S
6/**
7 * A thread worker with message channels for communication between main thread and thread worker.
8 */
c97c7edb 9export type ThreadWorkerWithMessageChannel = Worker & Draft<MessageChannel>
4ade5f1f
S
10
11/**
729c563d
S
12 * A thread pool with a fixed number of threads.
13 *
14 * It is possible to perform tasks in sync or asynchronous mode as you prefer.
15 *
16 * This pool selects the threads in a round robin fashion.
4ade5f1f 17 *
729c563d
S
18 * @template Data Type of data sent to the worker.
19 * @template Response Type of response of execution.
4ade5f1f
S
20 *
21 * @author [Alessandro Pio Ardizio](https://github.com/pioardi)
22 * @since 0.0.1
23 */
d3c8a1a8
S
24export class FixedThreadPool<
25 Data extends JSONValue = JSONValue,
26 Response extends JSONValue = JSONValue
27> extends AbstractPool<ThreadWorkerWithMessageChannel, Data, Response> {
4ade5f1f 28 /**
729c563d
S
29 * Constructs a new poolifier fixed thread pool.
30 *
31 * @param numThreads Number of threads for this pool.
32 * @param filePath Path to an implementation of a `ThreadWorker` file, which can be relative or absolute.
33 * @param opts Options for this fixed thread pool. Default: `{ maxTasks: 1000 }`
4ade5f1f
S
34 */
35 public constructor (
c97c7edb
S
36 numThreads: number,
37 filePath: string,
38 opts: PoolOptions<ThreadWorkerWithMessageChannel> = { maxTasks: 1000 }
4ade5f1f 39 ) {
c97c7edb
S
40 super(numThreads, filePath, opts)
41 }
4ade5f1f 42
c97c7edb
S
43 protected isMain (): boolean {
44 return isMainThread
4ade5f1f
S
45 }
46
c97c7edb
S
47 protected async destroyWorker (
48 worker: ThreadWorkerWithMessageChannel
49 ): Promise<void> {
50 await worker.terminate()
f2fdaa86 51 // FIXME: The tests are currently failing, so these must be changed first
4ade5f1f
S
52 }
53
c97c7edb
S
54 protected sendToWorker (
55 worker: ThreadWorkerWithMessageChannel,
56 message: MessageValue<Data>
57 ): void {
58 worker.postMessage(message)
4ade5f1f
S
59 }
60
c97c7edb
S
61 protected registerWorkerMessageListener (
62 port: ThreadWorkerWithMessageChannel,
63 listener: (message: MessageValue<Response>) => void
64 ): void {
65 port.port2?.on('message', listener)
4ade5f1f
S
66 }
67
c97c7edb
S
68 protected unregisterWorkerMessageListener (
69 port: ThreadWorkerWithMessageChannel,
70 listener: (message: MessageValue<Response>) => void
71 ): void {
72 port.port2?.removeListener('message', listener)
4ade5f1f
S
73 }
74
c97c7edb
S
75 protected newWorker (): ThreadWorkerWithMessageChannel {
76 return new Worker(this.filePath, {
4ade5f1f
S
77 env: SHARE_ENV
78 })
c97c7edb
S
79 }
80
81 protected afterNewWorkerPushed (
82 worker: ThreadWorkerWithMessageChannel
83 ): void {
4ade5f1f
S
84 const { port1, port2 } = new MessageChannel()
85 worker.postMessage({ parent: port1 }, [port1])
86 worker.port1 = port1
87 worker.port2 = port2
88 // we will attach a listener for every task,
89 // when task is completed the listener will be removed but to avoid warnings we are increasing the max listeners size
ee99693b 90 worker.port2.setMaxListeners(this.opts.maxTasks ?? 1000)
4ade5f1f
S
91 }
92}