fix: prepare code to fix pool internal IPC for cluster worker
[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 *
32 * It is possible to perform tasks in sync or asynchronous mode as you prefer.
33 *
e102732c
JB
34 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
35 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
4ade5f1f
S
36 * @author [Alessandro Pio Ardizio](https://github.com/pioardi)
37 * @since 0.0.1
38 */
d3c8a1a8 39export class FixedThreadPool<
deb85c12
JB
40 Data = unknown,
41 Response = unknown
e102732c 42> extends AbstractPool<Worker, Data, Response> {
4ade5f1f 43 /**
729c563d
S
44 * Constructs a new poolifier fixed thread pool.
45 *
38e795c1
JB
46 * @param numberOfThreads - Number of threads for this pool.
47 * @param filePath - Path to an implementation of a `ThreadWorker` file, which can be relative or absolute.
48 * @param opts - Options for this fixed thread pool.
4ade5f1f
S
49 */
50 public constructor (
5c5a1fb7 51 numberOfThreads: number,
c97c7edb 52 filePath: string,
90082c8c 53 protected readonly opts: ThreadPoolOptions = {}
4ade5f1f 54 ) {
5c5a1fb7 55 super(numberOfThreads, filePath, opts)
c97c7edb 56 }
4ade5f1f 57
afc003b2 58 /** @inheritDoc */
c97c7edb
S
59 protected isMain (): boolean {
60 return isMainThread
4ade5f1f
S
61 }
62
afc003b2 63 /** @inheritDoc */
e102732c 64 protected async destroyWorker (worker: Worker): Promise<void> {
cefac5ba 65 this.sendToWorker(worker, { kill: 1 })
c97c7edb 66 await worker.terminate()
4ade5f1f
S
67 }
68
afc003b2 69 /** @inheritDoc */
e102732c 70 protected sendToWorker (worker: Worker, message: MessageValue<Data>): void {
c97c7edb 71 worker.postMessage(message)
4ade5f1f
S
72 }
73
afc003b2 74 /** @inheritDoc */
e102732c 75 protected createWorker (): Worker {
c97c7edb 76 return new Worker(this.filePath, {
90082c8c
JB
77 env: SHARE_ENV,
78 ...this.opts.workerOptions
4ade5f1f 79 })
c97c7edb
S
80 }
81
afc003b2 82 /** @inheritDoc */
8881ae32 83 protected get type (): PoolType {
6b27d407 84 return PoolTypes.fixed
7c0ba920
JB
85 }
86
184855e6
JB
87 /** @inheritDoc */
88 protected get worker (): WorkerType {
89 return WorkerTypes.thread
90 }
91
08f3f44c 92 /** @inheritDoc */
6b27d407
JB
93 protected get minSize (): number {
94 return this.numberOfWorkers
95 }
96
97 /** @inheritDoc */
98 protected get maxSize (): number {
08f3f44c
JB
99 return this.numberOfWorkers
100 }
101
afc003b2 102 /** @inheritDoc */
c319c66b 103 protected get busy (): boolean {
c2ade475 104 return this.internalBusy()
7c0ba920 105 }
4ade5f1f 106}