-
Notifications
You must be signed in to change notification settings - Fork 1
/
worker_manager.ts
167 lines (151 loc) · 4.29 KB
/
worker_manager.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
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
/**
* MTKruto Server
* Copyright (C) 2024 Roj <https://roj.im/>
*
* This file is part of MTKruto Server.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import * as fs from "std/fs/mod.ts";
import * as path from "std/path/mod.ts";
import { Mutex } from "mtkruto/1_utilities.ts";
import type { Handler, WorkerStats } from "./worker.ts";
import { ClientManager } from "./client_manager.ts";
const workerUrl = path.toFileUrl(
path.join(path.dirname(path.fromFileUrl(import.meta.url)), "worker.ts"),
);
interface Worker_ {
worker: Worker;
promises: Map<string, (result: any) => void>;
}
export class WorkerManager {
#workers = new Array<Worker_>();
#map = new Map<string, number>();
#mutexes = new Map<string, Mutex>();
#registerListeners(id: number) {
const { worker, promises } = this.get(id);
worker.addEventListener("message", (e) => {
const [_id, result] = e.data;
promises.get(_id)?.(result);
promises.delete(_id);
});
}
create() {
const id = this.#workers.push({
worker: new Worker(workerUrl, { type: "module" }),
promises: new Map(),
}) - 1;
this.#registerListeners(id);
return id;
}
get(id: number) {
return this.#workers[id];
}
count() {
return this.#workers.length;
}
call<M extends keyof Handler>(
id: number,
_: M,
...args: Parameters<Handler[M]>
): Promise<Awaited<ReturnType<Handler[M]>>> {
const { worker, promises } = this.get(id);
const _id = crypto.randomUUID();
const promise = new Promise<any>((r) => {
promises.set(_id, r);
});
worker.postMessage([_id, { _, args }]);
return promise;
}
async #getNextWorker() {
let prevCount = 0;
let prevI = 0;
for (let i = 0; i < this.count(); i++) {
const count = await this.call(i, "clientCount");
if (count == 0) {
return i;
}
if (i == 0 || count < prevCount) {
prevCount = count;
prevI = i;
}
if (prevCount == 0) {
return i;
}
}
return prevI;
}
async getClientWorker(id: string) {
let worker = this.#map.get(id);
if (worker !== undefined) {
return worker;
}
let mutex = this.#mutexes.get(id);
if (!mutex) {
mutex = new Mutex();
this.#mutexes.set(id, mutex);
}
const unlock = await mutex.lock();
try {
worker = this.#map.get(id);
if (worker !== undefined) {
return worker;
}
worker = await this.#getNextWorker();
this.#map.set(id, worker);
return worker;
} finally {
unlock();
this.#mutexes.delete(id);
}
}
getStats() {
const stats = new Array<Promise<WorkerStats>>();
for (const worker of this.#workers.keys()) {
stats.push(this.call(worker, "stats"));
}
return Promise.all(stats);
}
async startWebhookLoops() {
let started = 0;
if (await fs.exists(ClientManager.KV_PATH)) {
for await (const entry of Deno.readDir(ClientManager.KV_PATH)) {
if (!entry.isFile) {
continue;
}
const id = entry.name;
let kv: Deno.Kv;
try {
kv = await Deno.openKv(path.join(ClientManager.KV_PATH, id));
} catch {
continue;
}
const webhook = await kv.get([ClientManager.WEBHOOK]);
if (webhook.value != null) {
const worker = await this.getClientWorker(id);
await this.call(worker, "startWebhookLoop", id);
++started;
}
}
}
return started;
}
async unload() {
const promises = new Array<Promise<void>>();
for (const worker of this.#workers.keys()) {
promises.push(this.call(worker, "unload"));
}
await Promise.all(promises);
}
}