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