chore: v2.5.2
[poolifier.git] / src / pools / thread / fixed.ts
CommitLineData
fc3e6586 1import {
fc3e6586
JB
2 MessageChannel,
3 SHARE_ENV,
65d7a1c9
JB
4 Worker,
5 isMainThread
fc3e6586 6} from 'node:worker_threads'
deb85c12 7import type { Draft, MessageValue } from '../../utility-types'
c97c7edb 8import { AbstractPool } from '../abstract-pool'
6b27d407 9import { type PoolOptions, type PoolType, PoolTypes } from '../pool'
4ade5f1f 10
729c563d
S
11/**
12 * A thread worker with message channels for communication between main thread and thread worker.
13 */
c97c7edb 14export type ThreadWorkerWithMessageChannel = Worker & Draft<MessageChannel>
4ade5f1f
S
15
16/**
729c563d
S
17 * A thread pool with a fixed number of threads.
18 *
19 * It is possible to perform tasks in sync or asynchronous mode as you prefer.
20 *
21 * This pool selects the threads in a round robin fashion.
4ade5f1f 22 *
38e795c1 23 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
02706357 24 * @typeParam Response - Type of execution response. This can only be serializable data.
4ade5f1f
S
25 * @author [Alessandro Pio Ardizio](https://github.com/pioardi)
26 * @since 0.0.1
27 */
d3c8a1a8 28export class FixedThreadPool<
deb85c12
JB
29 Data = unknown,
30 Response = unknown
d3c8a1a8 31> extends AbstractPool<ThreadWorkerWithMessageChannel, Data, Response> {
4ade5f1f 32 /**
729c563d
S
33 * Constructs a new poolifier fixed thread pool.
34 *
38e795c1
JB
35 * @param numberOfThreads - Number of threads for this pool.
36 * @param filePath - Path to an implementation of a `ThreadWorker` file, which can be relative or absolute.
37 * @param opts - Options for this fixed thread pool.
4ade5f1f
S
38 */
39 public constructor (
5c5a1fb7 40 numberOfThreads: number,
c97c7edb 41 filePath: string,
1927ee67 42 opts: PoolOptions<ThreadWorkerWithMessageChannel> = {}
4ade5f1f 43 ) {
5c5a1fb7 44 super(numberOfThreads, filePath, opts)
c97c7edb 45 }
4ade5f1f 46
afc003b2 47 /** @inheritDoc */
c97c7edb
S
48 protected isMain (): boolean {
49 return isMainThread
4ade5f1f
S
50 }
51
afc003b2 52 /** @inheritDoc */
14a2e530 53 protected async destroyWorker (
c97c7edb
S
54 worker: ThreadWorkerWithMessageChannel
55 ): Promise<void> {
cefac5ba 56 this.sendToWorker(worker, { kill: 1 })
c97c7edb 57 await worker.terminate()
4ade5f1f
S
58 }
59
afc003b2 60 /** @inheritDoc */
c97c7edb
S
61 protected sendToWorker (
62 worker: ThreadWorkerWithMessageChannel,
63 message: MessageValue<Data>
64 ): void {
65 worker.postMessage(message)
4ade5f1f
S
66 }
67
afc003b2 68 /** @inheritDoc */
c319c66b 69 protected registerWorkerMessageListener<Message extends Data | Response>(
ef41a6e6 70 worker: ThreadWorkerWithMessageChannel,
4f7fa42a 71 listener: (message: MessageValue<Message>) => void
c97c7edb 72 ): void {
ef41a6e6 73 worker.port2?.on('message', listener)
4ade5f1f
S
74 }
75
afc003b2 76 /** @inheritDoc */
280c2a77 77 protected createWorker (): ThreadWorkerWithMessageChannel {
c97c7edb 78 return new Worker(this.filePath, {
4ade5f1f
S
79 env: SHARE_ENV
80 })
c97c7edb
S
81 }
82
afc003b2 83 /** @inheritDoc */
280c2a77 84 protected afterWorkerSetup (worker: ThreadWorkerWithMessageChannel): void {
4ade5f1f
S
85 const { port1, port2 } = new MessageChannel()
86 worker.postMessage({ parent: port1 }, [port1])
87 worker.port1 = port1
88 worker.port2 = port2
a05c10de 89 // Listen to worker messages.
be0676b3 90 this.registerWorkerMessageListener(worker, super.workerListener())
4ade5f1f 91 }
7c0ba920 92
afc003b2 93 /** @inheritDoc */
7c0ba920 94 public get type (): PoolType {
6b27d407 95 return PoolTypes.fixed
7c0ba920
JB
96 }
97
08f3f44c 98 /** @inheritDoc */
6b27d407
JB
99 protected get minSize (): number {
100 return this.numberOfWorkers
101 }
102
103 /** @inheritDoc */
104 protected get maxSize (): number {
08f3f44c
JB
105 return this.numberOfWorkers
106 }
107
afc003b2 108 /** @inheritDoc */
c319c66b 109 protected get full (): boolean {
1f68cede 110 return this.workerNodes.length >= this.numberOfWorkers
c2ade475
JB
111 }
112
afc003b2 113 /** @inheritDoc */
c319c66b 114 protected get busy (): boolean {
c2ade475 115 return this.internalBusy()
7c0ba920 116 }
4ade5f1f 117}