refactor(api): ♻️ Use Web Workers instead of spawning the same process once for each thread

This commit is contained in:
Jesse Wierzbinski 2024-06-26 14:44:08 -10:00
parent bc8220c8f9
commit d29603275a
No known key found for this signature in database
7 changed files with 213 additions and 164 deletions

View file

@ -10,6 +10,8 @@ import {
import {
type LogLevel,
type LogRecord,
type RotatingFileSinkOptions,
type Sink,
configure,
getConsoleSink,
getLevelFilter,
@ -21,9 +23,48 @@ import { config } from "~/packages/config-manager";
// HACK: This is a workaround for the lack of type exports in the Logtape package.
type RotatingFileSinkDriver<T> =
import("../node_modules/@logtape/logtape/logtape/sink").RotatingFileSinkDriver<T>;
const getBaseRotatingFileSink = (
await import("../node_modules/@logtape/logtape/logtape/sink")
).getRotatingFileSink;
// HACK: Stolen
export function getBaseRotatingFileSink<TFile>(
path: string,
options: RotatingFileSinkOptions & RotatingFileSinkDriver<TFile>,
): Sink & Disposable {
const formatter = options.formatter ?? defaultTextFormatter;
const encoder = options.encoder ?? new TextEncoder();
const maxSize = options.maxSize ?? 1024 * 1024;
const maxFiles = options.maxFiles ?? 5;
let { size: offset } = options.statSync(path);
let fd = options.openSync(path);
function shouldRollover(bytes: Uint8Array): boolean {
return offset + bytes.length > maxSize;
}
function performRollover(): void {
options.closeSync(fd);
for (let i = maxFiles - 1; i > 0; i--) {
const oldPath = `${path}.${i}`;
const newPath = `${path}.${i + 1}`;
try {
options.renameSync(oldPath, newPath);
} catch (_) {
// Continue if the file does not exist.
}
}
options.renameSync(path, `${path}.1`);
offset = 0;
fd = options.openSync(path);
}
const sink: Sink & Disposable = (record: LogRecord) => {
const bytes = encoder.encode(formatter(record));
if (shouldRollover(bytes)) {
performRollover();
}
options.writeSync(fd, bytes);
options.flushSync(fd);
offset += bytes.length;
};
sink[Symbol.dispose] = () => options.closeSync(fd);
return sink;
}
const levelAbbreviations: Record<LogLevel, string> = {
debug: "DBG",
@ -149,8 +190,8 @@ export const configureLoggers = (silent = false) =>
},
filters: {
configFilter: silent
? getLevelFilter(config.logging.log_level)
: getLevelFilter(null),
? getLevelFilter(null)
: getLevelFilter(config.logging.log_level),
},
loggers: [
{

22
utils/server.ts Normal file
View file

@ -0,0 +1,22 @@
import type { Config } from "config-manager";
import type { Hono } from "hono";
export const createServer = (config: Config, app: Hono) =>
Bun.serve({
port: config.http.bind_port,
reusePort: true,
tls: config.http.tls.enabled
? {
key: Bun.file(config.http.tls.key),
cert: Bun.file(config.http.tls.cert),
passphrase: config.http.tls.passphrase,
ca: config.http.tls.ca
? Bun.file(config.http.tls.ca)
: undefined,
}
: undefined,
hostname: config.http.bind || "0.0.0.0", // defaults to "0.0.0.0"
fetch(req, server) {
return app.fetch(req, { ip: server.requestIP(req) });
},
});