-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworker-pool.ts
More file actions
167 lines (148 loc) · 5.97 KB
/
worker-pool.ts
File metadata and controls
167 lines (148 loc) · 5.97 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { v4 as uuidv4 } from 'uuid';
import { logger } from '../logger';
import { isHeartbeatMessage, isWorkerMessageWithId, type WorkerMessage, type WorkerMessageWithId } from './messages';
type PromiseResolve<T extends WorkerMessageWithId> = (t: T) => void;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
// biome-ignore lint/suspicious/noExplicitAny: This is aligned with the standard Promise API
type PromiseReject = (reason: any) => void;
type MessageValidator<T> = TypeGuardFunction<unknown, T>;
type TypeGuardFunction<T, S extends T> = (value: T) => value is S;
type MessagePromise = {
validator: MessageValidator<WorkerMessageWithId>;
resolve: PromiseResolve<WorkerMessageWithId>;
reject: PromiseReject;
promise: Promise<WorkerMessageWithId>;
};
export enum WorkerStatus {
Available = 'Available',
Unresponsive = 'Unresponsive',
}
export class WorkerPool {
#workers: Worker[];
#promises: Map<string, MessagePromise>;
#timeOfPreviousHeartbeat: Map<number, number>;
#which: number;
constructor(size: number, workerModule: URL) {
this.#workers = new Array(size);
this.#timeOfPreviousHeartbeat = new Map();
for (let i = 0; i < size; i++) {
this.#workers[i] = new Worker(workerModule, { type: 'module' });
this.#workers[i].onmessage = (msg) => this.#handleMessage(i, msg);
this.#timeOfPreviousHeartbeat.set(i, 0);
}
this.#promises = new Map();
this.#which = 0;
}
/**
* Warning - nothing in this class should be considered useable after
* calling this method - any/all methods called should be expected to be
* completely unreliable. dont call me unless you're about to dispose of all references to this object
*/
destroy() {
for (let i = 0; i < this.#workers.length; i++) {
this.#workers[i].terminate();
}
this.#workers = [];
}
#handleMessage(workerIndex: number, msg: MessageEvent<unknown>) {
const { data } = msg;
if (isHeartbeatMessage(data)) {
this.#timeOfPreviousHeartbeat.set(workerIndex, Date.now());
return;
}
if (isWorkerMessageWithId(data)) {
logger.debug(`worker ${workerIndex} responded to a message`);
const { id } = data;
const messagePromise = this.#promises.get(id);
if (messagePromise === undefined) {
logger.warn('unexpected message from worker');
return;
}
this.#promises.delete(id);
if (!messagePromise.validator(data)) {
const reason = 'invalid response from worker: message type did not match expected type';
logger.error(reason);
messagePromise.reject(new Error(reason));
return;
}
messagePromise.resolve(data);
} else {
logger.debug(`worker ${workerIndex} received an invalid message`);
const reason = 'encountered an invalid message; skipping';
logger.warn(reason);
}
}
#roundRobin() {
this.#which = (this.#which + 1) % this.#workers.length;
}
async submitRequest(
message: WorkerMessage,
responseValidator: MessageValidator<WorkerMessageWithId>,
transfers: Transferable[],
signal?: AbortSignal | undefined,
): Promise<WorkerMessageWithId> {
if (this.#workers.length < 1) {
return Promise.reject('this woorker pool has been disposed');
}
const reqId = `rq${uuidv4()}`;
const workerIndex = this.#which;
const messageWithId = { ...message, id: reqId };
const messagePromise = this.#createMessagePromise(responseValidator);
logger.debug(`worker ${workerIndex} being handed a request`);
this.#promises.set(reqId, messagePromise);
if (signal) {
signal.addEventListener('abort', () => {
this.#sendMessageToWorker(workerIndex, { type: 'cancel', id: reqId }, []);
messagePromise.reject('cancelled');
});
}
this.#sendMessageToWorker(workerIndex, messageWithId, transfers);
this.#roundRobin();
return messagePromise.promise;
}
#sendMessageToWorker(workerIndex: number, message: WorkerMessageWithId, transfers: Transferable[]) {
const worker = this.#workers[workerIndex];
if (worker === undefined) {
throw new Error('cannot send message to worker: index invalid');
}
worker.postMessage(message, transfers);
}
#createMessagePromise(responseValidator: MessageValidator<WorkerMessageWithId>): MessagePromise {
const { promise, resolve, reject } = Promise.withResolvers<WorkerMessageWithId>();
return {
validator: responseValidator,
resolve,
reject,
promise,
};
}
#isValidIndex(index: number): boolean {
const len = this.#workers.length;
return index < len && index >= 0;
}
getStatus(workerIndex: number): WorkerStatus {
if (!this.#isValidIndex(workerIndex)) {
throw new Error('invalid worker index');
}
const lastHeartbeat = this.#timeOfPreviousHeartbeat.get(workerIndex) ?? 0;
if (lastHeartbeat === 0) {
return WorkerStatus.Unresponsive;
}
const delta = Date.now() - lastHeartbeat;
if (delta && delta > 1500) {
return WorkerStatus.Unresponsive;
}
return WorkerStatus.Available;
}
getStatuses(): ReadonlyMap<number, WorkerStatus> {
return this.#workers.reduce<Map<number, WorkerStatus>>((acc, _w, i) => {
const delta = Date.now() - (this.#timeOfPreviousHeartbeat.get(i) ?? 0);
if (delta && delta > 1500) {
acc.set(i, WorkerStatus.Unresponsive);
} else {
acc.set(i, WorkerStatus.Available);
}
return acc;
}, new Map());
}
}