Commit | Line | Data |
---|---|---|
fc3e6586 | 1 | import { |
85aeb3f3 JB |
2 | type MessageChannel, |
3 | type MessagePort, | |
fc3e6586 | 4 | SHARE_ENV, |
7d91a8cd | 5 | type TransferListItem, |
65d7a1c9 | 6 | Worker, |
90082c8c | 7 | type WorkerOptions, |
65d7a1c9 | 8 | isMainThread |
fc3e6586 | 9 | } from 'node:worker_threads' |
e102732c | 10 | import type { MessageValue } from '../../utility-types' |
c97c7edb | 11 | import { AbstractPool } from '../abstract-pool' |
4b628b48 JB |
12 | import { type PoolOptions, type PoolType, PoolTypes } from '../pool' |
13 | import { type WorkerType, WorkerTypes } from '../worker' | |
4ade5f1f | 14 | |
90082c8c JB |
15 | /** |
16 | * Options for a poolifier thread pool. | |
17 | */ | |
18 | export interface ThreadPoolOptions extends PoolOptions<Worker> { | |
19 | /** | |
20 | * Worker options. | |
21 | * | |
22 | * @see https://nodejs.org/api/worker_threads.html#new-workerfilename-options | |
23 | */ | |
24 | workerOptions?: WorkerOptions | |
25 | } | |
26 | ||
4ade5f1f | 27 | /** |
729c563d S |
28 | * A thread pool with a fixed number of threads. |
29 | * | |
e102732c JB |
30 | * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data. |
31 | * @typeParam Response - Type of execution response. This can only be structured-cloneable data. | |
4ade5f1f S |
32 | * @author [Alessandro Pio Ardizio](https://github.com/pioardi) |
33 | * @since 0.0.1 | |
34 | */ | |
d3c8a1a8 | 35 | export class FixedThreadPool< |
deb85c12 JB |
36 | Data = unknown, |
37 | Response = unknown | |
e102732c | 38 | > extends AbstractPool<Worker, Data, Response> { |
4ade5f1f | 39 | /** |
729c563d S |
40 | * Constructs a new poolifier fixed thread pool. |
41 | * | |
38e795c1 JB |
42 | * @param numberOfThreads - Number of threads for this pool. |
43 | * @param filePath - Path to an implementation of a `ThreadWorker` file, which can be relative or absolute. | |
44 | * @param opts - Options for this fixed thread pool. | |
4ade5f1f S |
45 | */ |
46 | public constructor ( | |
5c5a1fb7 | 47 | numberOfThreads: number, |
c97c7edb | 48 | filePath: string, |
90082c8c | 49 | protected readonly opts: ThreadPoolOptions = {} |
4ade5f1f | 50 | ) { |
5c5a1fb7 | 51 | super(numberOfThreads, filePath, opts) |
c97c7edb | 52 | } |
4ade5f1f | 53 | |
afc003b2 | 54 | /** @inheritDoc */ |
c97c7edb S |
55 | protected isMain (): boolean { |
56 | return isMainThread | |
4ade5f1f S |
57 | } |
58 | ||
afc003b2 | 59 | /** @inheritDoc */ |
aa9eede8 | 60 | protected async destroyWorkerNode (workerNodeKey: number): Promise<void> { |
81c02522 JB |
61 | this.flushTasksQueue(workerNodeKey) |
62 | // FIXME: wait for tasks to be finished | |
aa9eede8 JB |
63 | const workerNode = this.workerNodes[workerNodeKey] |
64 | const worker = workerNode.worker | |
041dc05b | 65 | const waitWorkerExit = new Promise<void>(resolve => { |
81c02522 JB |
66 | worker.on('exit', () => { |
67 | resolve() | |
68 | }) | |
69 | }) | |
75de9f41 JB |
70 | await this.sendKillMessageToWorker( |
71 | workerNodeKey, | |
72 | workerNode.info.id as number | |
73 | ) | |
aa9eede8 | 74 | workerNode.closeChannel() |
c97c7edb | 75 | await worker.terminate() |
c2301b8e | 76 | await waitWorkerExit |
4ade5f1f S |
77 | } |
78 | ||
afc003b2 | 79 | /** @inheritDoc */ |
aa9eede8 JB |
80 | protected sendToWorker ( |
81 | workerNodeKey: number, | |
7d91a8cd JB |
82 | message: MessageValue<Data>, |
83 | transferList?: TransferListItem[] | |
aa9eede8 | 84 | ): void { |
041dc05b | 85 | ( |
7884d183 | 86 | this.workerNodes[workerNodeKey].messageChannel as MessageChannel |
7d91a8cd | 87 | ).port1.postMessage(message, transferList) |
85aeb3f3 JB |
88 | } |
89 | ||
90 | /** @inheritDoc */ | |
aa9eede8 | 91 | protected sendStartupMessageToWorker (workerNodeKey: number): void { |
75de9f41 JB |
92 | const workerNode = this.workerNodes[workerNodeKey] |
93 | const worker = workerNode.worker | |
94 | const port2: MessagePort = (workerNode.messageChannel as MessageChannel) | |
95 | .port2 | |
85aeb3f3 JB |
96 | worker.postMessage( |
97 | { | |
98 | ready: false, | |
75de9f41 | 99 | workerId: workerNode.info.id, |
85aeb3f3 JB |
100 | port: port2 |
101 | }, | |
102 | [port2] | |
103 | ) | |
104 | } | |
105 | ||
106 | /** @inheritDoc */ | |
107 | protected registerWorkerMessageListener<Message extends Data | Response>( | |
aa9eede8 | 108 | workerNodeKey: number, |
85aeb3f3 JB |
109 | listener: (message: MessageValue<Message>) => void |
110 | ): void { | |
041dc05b | 111 | ( |
7884d183 | 112 | this.workerNodes[workerNodeKey].messageChannel as MessageChannel |
85aeb3f3 | 113 | ).port1.on('message', listener) |
4ade5f1f S |
114 | } |
115 | ||
afc003b2 | 116 | /** @inheritDoc */ |
e102732c | 117 | protected createWorker (): Worker { |
c97c7edb | 118 | return new Worker(this.filePath, { |
90082c8c JB |
119 | env: SHARE_ENV, |
120 | ...this.opts.workerOptions | |
4ade5f1f | 121 | }) |
c97c7edb S |
122 | } |
123 | ||
afc003b2 | 124 | /** @inheritDoc */ |
8881ae32 | 125 | protected get type (): PoolType { |
6b27d407 | 126 | return PoolTypes.fixed |
7c0ba920 JB |
127 | } |
128 | ||
184855e6 JB |
129 | /** @inheritDoc */ |
130 | protected get worker (): WorkerType { | |
131 | return WorkerTypes.thread | |
132 | } | |
133 | ||
afc003b2 | 134 | /** @inheritDoc */ |
c319c66b | 135 | protected get busy (): boolean { |
c2ade475 | 136 | return this.internalBusy() |
7c0ba920 | 137 | } |
4ade5f1f | 138 | } |