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