feat: add async SQLite on Node 24 and improve messaging diagnostics (#520)
Move database work off the main thread on Node.js 24 and improve message handling and runtime diagnostics. Preserve migration, restart, and concurrency coverage with the async SQLite driver.
This commit is contained in:
1 parent
20f48d11ec
commit
c7428363cb
170 files changed
+7748
-3913
No files matched your search
@@ -10,7 +10,6 @@ import { stageSourceToDisk } from '@snowluma/protocol/highway/stage';
|
||||
import { hashFileStreaming } from '@snowluma/protocol/highway/hash-file';
|
||||
import { protobuf_encode } from '@snowluma/proton';
|
||||
import { promises as fsp } from 'fs';
|
||||
import type { Bridge } from '../bridge';
|
||||
import type { BridgeContext } from '../bridge-context';
|
||||
import { resolveSelfUid, toInt, type MediaIndexNode } from './shared';
|
||||
|
||||
@@ -40,10 +39,6 @@ import { ensureRetCodeZero } from '@snowluma/protocol/oidb-services/shared';
|
||||
|
||||
const log = createLogger('GroupFile');
|
||||
|
||||
function asBridge(ctx: BridgeContext): Bridge {
|
||||
return ctx as unknown as Bridge;
|
||||
}
|
||||
|
||||
// ─────────────── public result types ───────────────
|
||||
|
||||
export interface GroupFileInfo {
|
||||
@@ -296,7 +291,7 @@ export class GroupFileApi {
|
||||
uploadFile = true,
|
||||
publishFile = uploadFile,
|
||||
): Promise<UploadFileResult> {
|
||||
const bridge = asBridge(this.ctx);
|
||||
const bridge = this.ctx;
|
||||
// Group/private files may legitimately be up to 4 GiB on QQ's wire, so
|
||||
// override the default 1 GiB cap with the protocol ceiling. Stage onto a
|
||||
// local disk path and stream-hash it — the (up to 4 GiB) file is never
|
||||
@@ -430,7 +425,7 @@ export class GroupFileApi {
|
||||
uploadFile = true,
|
||||
publishFile = uploadFile,
|
||||
): Promise<UploadFileResult> {
|
||||
const bridge = asBridge(this.ctx);
|
||||
const bridge = this.ctx;
|
||||
// Stage onto a local disk path + stream-hash — the (up to 4 GiB) file is
|
||||
// never buffered in RAM. headLimit = MD5_HEAD_LIMIT yields the offline-file
|
||||
// md510MCheckSum in the same single pass.
|
||||
@@ -724,8 +719,7 @@ export class GroupFileApi {
|
||||
}
|
||||
|
||||
async getPrivateUrl(userId: number, fileId: string, fileHash: string): Promise<string> {
|
||||
const bridge = asBridge(this.ctx);
|
||||
const selfUid = await resolveSelfUid(bridge);
|
||||
const selfUid = await resolveSelfUid(this.ctx);
|
||||
void userId;
|
||||
return GetPrivateFileUrl.invoke(this.ctx, { selfUid, fileId, fileHash });
|
||||
}
|
||||
@@ -784,8 +778,7 @@ export class GroupFileApi {
|
||||
}
|
||||
|
||||
async getPrivatePttUrl(node: MediaIndexNode): Promise<string> {
|
||||
const bridge = asBridge(this.ctx);
|
||||
const selfUid = await resolveSelfUid(bridge);
|
||||
const selfUid = await resolveSelfUid(this.ctx);
|
||||
return GetPrivatePttUrl.invoke(this.ctx, { selfUid, node });
|
||||
}
|
||||
|
||||
@@ -794,8 +787,7 @@ export class GroupFileApi {
|
||||
}
|
||||
|
||||
async getPrivateVideoUrl(node: MediaIndexNode): Promise<string> {
|
||||
const bridge = asBridge(this.ctx);
|
||||
const selfUid = await resolveSelfUid(bridge);
|
||||
const selfUid = await resolveSelfUid(this.ctx);
|
||||
return GetPrivateVideoUrl.invoke(this.ctx, { selfUid, node });
|
||||
}
|
||||
}
|
||||
@@ -1,20 +1,4 @@
|
||||
export function toInt(value: unknown): number {
|
||||
if (typeof value === 'number' && Number.isFinite(value)) {
|
||||
return Math.trunc(value);
|
||||
}
|
||||
|
||||
if (typeof value === 'bigint') {
|
||||
const n = Number(value);
|
||||
return Number.isFinite(n) ? Math.trunc(n) : 0;
|
||||
}
|
||||
|
||||
if (typeof value === 'string' && value.trim()) {
|
||||
const n = Number(value);
|
||||
return Number.isFinite(n) ? Math.trunc(n) : 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
export { toInt } from '@snowluma/protocol/oidb-services/shared';
|
||||
|
||||
// The bot's-own-UID resolver is the single source of truth in @snowluma/protocol
|
||||
// (a Bridge satisfies its BridgeContext slice). Re-exported here so the existing
|
||||
|
||||
@@ -37,6 +37,8 @@ export class Bridge implements BridgeInterface {
|
||||
private readonly sysMsgDedup_ = new SysMsgDedup();
|
||||
private onlineClients_: readonly Readonly<OnlineDeviceInfo>[] | null = null;
|
||||
private disposed_ = false;
|
||||
private disposePromise_: Promise<void> | null = null;
|
||||
private readonly pendingPackets_ = new Set<Promise<void>>();
|
||||
private rosterWarmupStarted_ = false;
|
||||
private rosterWarmupSettled_ = false;
|
||||
private rosterWarmupResult_: RosterWarmupResult | null = null;
|
||||
@@ -142,7 +144,11 @@ export class Bridge implements BridgeInterface {
|
||||
});
|
||||
}
|
||||
|
||||
dispose(): void {
|
||||
dispose(): Promise<void> {
|
||||
if (this.disposePromise_) {
|
||||
return this.disposePromise_;
|
||||
}
|
||||
|
||||
this.disposed_ = true;
|
||||
this.settleRosterWarmup({ friendsLoaded: false, groupsLoaded: false });
|
||||
this.pids_.clear();
|
||||
@@ -151,14 +157,15 @@ export class Bridge implements BridgeInterface {
|
||||
this.packetClient_ = null;
|
||||
this.packetClientPid_ = null;
|
||||
this.onlineClients_ = null;
|
||||
|
||||
try {
|
||||
this.identity.close();
|
||||
} finally {
|
||||
// A close failure must remain visible to the caller, but must not leave
|
||||
// event subscribers attached to a Bridge that Manager already removed.
|
||||
this.events.clear();
|
||||
}
|
||||
this.disposePromise_ = (async () => {
|
||||
try {
|
||||
await Promise.allSettled(this.pendingPackets_);
|
||||
await this.identity.close();
|
||||
} finally {
|
||||
this.events.clear();
|
||||
}
|
||||
})();
|
||||
return this.disposePromise_;
|
||||
}
|
||||
|
||||
/** Legacy two-step setter retained for external compatibility. It assigns
|
||||
@@ -303,7 +310,14 @@ export class Bridge implements BridgeInterface {
|
||||
}
|
||||
|
||||
onPacket(pkt: PacketInfo): Promise<void> {
|
||||
return this.pipeline.process(pkt);
|
||||
if (this.disposed_) {
|
||||
return Promise.reject(new Error('Bridge is disposed'));
|
||||
}
|
||||
|
||||
const work = this.identity.initialization.then(() => this.pipeline.process(pkt));
|
||||
this.pendingPackets_.add(work);
|
||||
void work.finally(() => this.pendingPackets_.delete(work)).catch(() => undefined);
|
||||
return work;
|
||||
}
|
||||
|
||||
rememberUploadedFile(meta: UploadedFileMeta): void {
|
||||
@@ -500,6 +514,12 @@ export class Bridge implements BridgeInterface {
|
||||
}
|
||||
|
||||
private async runRosterWarmup(): Promise<RosterWarmupResult> {
|
||||
await this.identity.initialization;
|
||||
|
||||
if (this.disposed_) {
|
||||
return { friendsLoaded: false, groupsLoaded: false };
|
||||
}
|
||||
|
||||
const uin = this.identity.uin;
|
||||
const selfUin = parseInt(uin, 10) || 0;
|
||||
let selfResolved = false;
|
||||
|
||||
@@ -19,6 +19,44 @@ const runtimeLog = createLogger('Bridge.Runtime');
|
||||
export class BridgeManager {
|
||||
private sessions_ = new Map<string, QQSession>();
|
||||
private pidToUin_ = new Map<number, string>();
|
||||
private readonly closingSessions_ = new Map<string, Promise<void>>();
|
||||
private readonly closeErrors_: unknown[] = [];
|
||||
|
||||
/** Hook callbacks remain synchronous; shutdown awaits every detached account. */
|
||||
async dispose(): Promise<void> {
|
||||
for (const [uin, session] of this.sessions_) {
|
||||
this.closeSession(uin, session.bridge);
|
||||
}
|
||||
|
||||
this.sessions_.clear();
|
||||
this.pidToUin_.clear();
|
||||
await Promise.allSettled(this.closingSessions_.values());
|
||||
|
||||
if (this.closeErrors_.length) {
|
||||
throw new AggregateError(this.closeErrors_, 'Bridge persistence shutdown failed');
|
||||
}
|
||||
}
|
||||
|
||||
private closeSession(uin: string, bridge: Bridge): void {
|
||||
const previous = this.closingSessions_.get(uin);
|
||||
const closing = Promise.all([previous, bridge.dispose()]).then(() => undefined);
|
||||
this.closingSessions_.set(uin, closing);
|
||||
void closing.then(
|
||||
() => {
|
||||
if (this.closingSessions_.get(uin) === closing) {
|
||||
this.closingSessions_.delete(uin);
|
||||
}
|
||||
},
|
||||
(error) => {
|
||||
this.closeErrors_.push(error);
|
||||
log.error(
|
||||
'identity close failed: UIN=%s err=%s',
|
||||
uin,
|
||||
error instanceof Error ? (error.stack ?? error.message) : String(error),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
private sessionStartedListeners_: SessionStartedCallback[] = [];
|
||||
private sessionClosedListeners_: SessionClosedCallback[] = [];
|
||||
@@ -378,7 +416,7 @@ export class BridgeManager {
|
||||
log.debug('session closed: UIN=%s', uin);
|
||||
// Fire before dispose() so listeners can still read bridge.identity.
|
||||
this.fireSessionClosed(uin, session.bridge);
|
||||
session.bridge.dispose();
|
||||
this.closeSession(uin, session.bridge);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -389,7 +427,9 @@ export class BridgeManager {
|
||||
return { session, created: false };
|
||||
}
|
||||
|
||||
const bridge = new Bridge(IdentityService.openForUin(uin));
|
||||
const bridge = new Bridge(
|
||||
IdentityService.openForUin(uin, 'data', this.closingSessions_.get(uin)),
|
||||
);
|
||||
session = { bridge };
|
||||
this.sessions_.set(uin, session);
|
||||
|
||||
|
||||
Reference in new issue
Block a user