-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
fixed.ts
137 lines (123 loc) · 3.64 KB
/
fixed.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import {
isMainThread,
type TransferListItem,
type Worker
} from 'node:worker_threads'
import type { MessageValue } from '../../utility-types.js'
import { AbstractPool } from '../abstract-pool.js'
import { type PoolOptions, type PoolType, PoolTypes } from '../pool.js'
import { type WorkerType, WorkerTypes } from '../worker.js'
/**
* Options for a poolifier thread pool.
*/
export type ThreadPoolOptions = PoolOptions<Worker>
/**
* A thread pool with a fixed number of threads.
*
* @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
* @typeParam Response - Type of execution response. This can only be structured-cloneable data.
* @author [Alessandro Pio Ardizio](https://github.com/pioardi)
* @since 0.0.1
*/
export class FixedThreadPool<
Data = unknown,
Response = unknown
> extends AbstractPool<Worker, Data, Response> {
/**
* Constructs a new poolifier fixed thread pool.
*
* @param numberOfThreads - Number of threads for this pool.
* @param filePath - Path to an implementation of a `ThreadWorker` file, which can be relative or absolute.
* @param opts - Options for this fixed thread pool.
*/
public constructor (
numberOfThreads: number,
filePath: string,
opts: ThreadPoolOptions = {},
maximumNumberOfThreads?: number
) {
super(numberOfThreads, filePath, opts, maximumNumberOfThreads)
}
/** @inheritDoc */
protected isMain (): boolean {
return isMainThread
}
/** @inheritDoc */
protected sendToWorker (
workerNodeKey: number,
message: MessageValue<Data>,
transferList?: readonly TransferListItem[]
): void {
this.workerNodes[workerNodeKey]?.messageChannel?.port1.postMessage(
{
...message,
workerId: this.getWorkerInfo(workerNodeKey)?.id
} satisfies MessageValue<Data>,
transferList
)
}
/** @inheritDoc */
protected sendStartupMessageToWorker (workerNodeKey: number): void {
const workerNode = this.workerNodes[workerNodeKey]
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const port2 = workerNode.messageChannel!.port2
workerNode.worker.postMessage(
{
ready: false,
workerId: this.getWorkerInfo(workerNodeKey)?.id,
port: port2
} satisfies MessageValue<Data>,
[port2]
)
}
/** @inheritDoc */
protected registerWorkerMessageListener<Message extends Data | Response>(
workerNodeKey: number,
listener: (message: MessageValue<Message>) => void
): void {
this.workerNodes[workerNodeKey].messageChannel?.port1.on(
'message',
listener
)
}
/** @inheritDoc */
protected registerOnceWorkerMessageListener<Message extends Data | Response>(
workerNodeKey: number,
listener: (message: MessageValue<Message>) => void
): void {
this.workerNodes[workerNodeKey].messageChannel?.port1.once(
'message',
listener
)
}
/** @inheritDoc */
protected deregisterWorkerMessageListener<Message extends Data | Response>(
workerNodeKey: number,
listener: (message: MessageValue<Message>) => void
): void {
this.workerNodes[workerNodeKey].messageChannel?.port1.off(
'message',
listener
)
}
/** @inheritDoc */
protected shallCreateDynamicWorker (): boolean {
return false
}
/** @inheritDoc */
protected checkAndEmitDynamicWorkerCreationEvents (): void {
/* noop */
}
/** @inheritDoc */
protected get type (): PoolType {
return PoolTypes.fixed
}
/** @inheritDoc */
protected get worker (): WorkerType {
return WorkerTypes.thread
}
/** @inheritDoc */
protected get busy (): boolean {
return this.internalBusy()
}
}