addd TG integration

This commit is contained in:
xCyanGrizzly
2026-03-02 11:57:17 +01:00
parent b427193d17
commit 4d0df6b1a4
35 changed files with 4436 additions and 242 deletions

40
worker/src/util/mutex.ts Normal file
View File

@@ -0,0 +1,40 @@
import { childLogger } from "./logger.js";
const log = childLogger("mutex");
let locked = false;
let holder = "";
const queue: Array<{ resolve: () => void; label: string }> = [];
/**
* Ensures only one TDLib client runs at a time across the entire worker process.
* Both the scheduler (auth, ingestion) and the fetch listener acquire this
* before creating any TDLib client.
*/
export async function withTdlibMutex<T>(
label: string,
fn: () => Promise<T>
): Promise<T> {
if (locked) {
log.info({ waiting: label, holder }, "Waiting for TDLib mutex");
await new Promise<void>((resolve) => queue.push({ resolve, label }));
}
locked = true;
holder = label;
log.debug({ label }, "TDLib mutex acquired");
try {
return await fn();
} finally {
locked = false;
holder = "";
const next = queue.shift();
if (next) {
log.debug({ next: next.label }, "TDLib mutex releasing to next waiter");
next.resolve();
} else {
log.debug({ label }, "TDLib mutex released");
}
}
}