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