refactor: cleanup internal pool messaging code
[poolifier.git] / src / pools / thread / fixed.ts
CommitLineData
fc3e6586 1import {
fc3e6586 2 SHARE_ENV,
65d7a1c9 3 Worker,
90082c8c 4 type WorkerOptions,
65d7a1c9 5 isMainThread
fc3e6586 6} from 'node:worker_threads'
e102732c 7import type { MessageValue } from '../../utility-types'
c97c7edb 8import { AbstractPool } from '../abstract-pool'
184855e6
JB
9import {
10 type PoolOptions,
11 type PoolType,
12 PoolTypes,
13 type WorkerType,
14 WorkerTypes
15} from '../pool'
4ade5f1f 16
90082c8c
JB
17/**
18 * Options for a poolifier thread pool.
19 */
20export interface ThreadPoolOptions extends PoolOptions<Worker> {
21 /**
22 * Worker options.
23 *
24 * @see https://nodejs.org/api/worker_threads.html#new-workerfilename-options
25 */
26 workerOptions?: WorkerOptions
27}
28
4ade5f1f 29/**
729c563d
S
30 * A thread pool with a fixed number of threads.
31 *
e102732c
JB
32 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
33 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
4ade5f1f
S
34 * @author [Alessandro Pio Ardizio](https://github.com/pioardi)
35 * @since 0.0.1
36 */
d3c8a1a8 37export class FixedThreadPool<
deb85c12
JB
38 Data = unknown,
39 Response = unknown
e102732c 40> extends AbstractPool<Worker, Data, Response> {
4ade5f1f 41 /**
729c563d
S
42 * Constructs a new poolifier fixed thread pool.
43 *
38e795c1
JB
44 * @param numberOfThreads - Number of threads for this pool.
45 * @param filePath - Path to an implementation of a `ThreadWorker` file, which can be relative or absolute.
46 * @param opts - Options for this fixed thread pool.
4ade5f1f
S
47 */
48 public constructor (
5c5a1fb7 49 numberOfThreads: number,
c97c7edb 50 filePath: string,
90082c8c 51 protected readonly opts: ThreadPoolOptions = {}
4ade5f1f 52 ) {
5c5a1fb7 53 super(numberOfThreads, filePath, opts)
c97c7edb 54 }
4ade5f1f 55
afc003b2 56 /** @inheritDoc */
c97c7edb
S
57 protected isMain (): boolean {
58 return isMainThread
4ade5f1f
S
59 }
60
afc003b2 61 /** @inheritDoc */
e102732c 62 protected async destroyWorker (worker: Worker): Promise<void> {
cefac5ba 63 this.sendToWorker(worker, { kill: 1 })
c97c7edb 64 await worker.terminate()
4ade5f1f
S
65 }
66
afc003b2 67 /** @inheritDoc */
e102732c 68 protected sendToWorker (worker: Worker, message: MessageValue<Data>): void {
c97c7edb 69 worker.postMessage(message)
4ade5f1f
S
70 }
71
afc003b2 72 /** @inheritDoc */
e102732c 73 protected createWorker (): Worker {
c97c7edb 74 return new Worker(this.filePath, {
90082c8c
JB
75 env: SHARE_ENV,
76 ...this.opts.workerOptions
4ade5f1f 77 })
c97c7edb
S
78 }
79
afc003b2 80 /** @inheritDoc */
8881ae32 81 protected get type (): PoolType {
6b27d407 82 return PoolTypes.fixed
7c0ba920
JB
83 }
84
184855e6
JB
85 /** @inheritDoc */
86 protected get worker (): WorkerType {
87 return WorkerTypes.thread
88 }
89
08f3f44c 90 /** @inheritDoc */
6b27d407
JB
91 protected get minSize (): number {
92 return this.numberOfWorkers
93 }
94
95 /** @inheritDoc */
96 protected get maxSize (): number {
08f3f44c
JB
97 return this.numberOfWorkers
98 }
99
afc003b2 100 /** @inheritDoc */
c319c66b 101 protected get busy (): boolean {
c2ade475 102 return this.internalBusy()
7c0ba920 103 }
4ade5f1f 104}