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
@@ -161,7 +161,7 @@ export class OneBotInstance {
|
||||
this.rkeyCache.resolveImageUrl(this.bridge, element, isGroup),
|
||||
mediaUrlResolver: (element, isGroup, sessionId) =>
|
||||
mediaUrlResolver.resolve(element, isGroup, sessionId),
|
||||
messageIdResolver: (
|
||||
messageIdResolver: async (
|
||||
isGroup,
|
||||
sessionId,
|
||||
sequence,
|
||||
@@ -178,7 +178,7 @@ export class OneBotInstance {
|
||||
(resolvedEventName === PRIVATE_MESSAGE_EVENT ||
|
||||
resolvedEventName === PRIVATE_SENT_MESSAGE_EVENT)
|
||||
) {
|
||||
const storedId = this.messageStore.resolvePrivateReplyMessageId(
|
||||
const storedId = await this.messageStore.resolvePrivateReplyMessageId(
|
||||
sessionId,
|
||||
sequence,
|
||||
resolvedEventName === PRIVATE_SENT_MESSAGE_EVENT,
|
||||
@@ -199,8 +199,8 @@ export class OneBotInstance {
|
||||
: resolvedEventName,
|
||||
);
|
||||
},
|
||||
mediaSegmentSink: (mediaType, element, data, isGroup, sessionId) =>
|
||||
mediaIndexer.remember(mediaType, element, data, isGroup, sessionId),
|
||||
mediaSegmentSink: async (mediaType, element, data, isGroup, sessionId) =>
|
||||
await mediaIndexer.remember(mediaType, element, data, isGroup, sessionId),
|
||||
};
|
||||
const ctx: OneBotInstanceContext = {
|
||||
uin: this.uin,
|
||||
@@ -214,7 +214,8 @@ export class OneBotInstance {
|
||||
config,
|
||||
musicSignUrl: globalSettings.musicSignUrl,
|
||||
getUptimeMs: () => Math.max(0, Date.now() - this.startedAt),
|
||||
cacheMessageMeta: (messageId, meta) => this.cacheMessageMeta(messageId, meta),
|
||||
cacheMessageMeta: async (messageId, meta) =>
|
||||
await this.cacheMessageMeta(messageId, meta),
|
||||
dispatchEvent: (event, source = 'bridge', startedAt) =>
|
||||
this.dispatchEvent(event, source, startedAt),
|
||||
};
|
||||
@@ -225,7 +226,12 @@ export class OneBotInstance {
|
||||
this.networkManager = new OneBotNetworkManager((desired) =>
|
||||
this.createNetworkAdapter(desired, networkCtx),
|
||||
);
|
||||
this.networkReady = this.networkManager.reconcile(config);
|
||||
this.networkReady = Promise.all([
|
||||
bridge.identity.initialization,
|
||||
this.messageStore.initialization,
|
||||
this.mediaStore.initialization,
|
||||
this.reactionStore.initialization,
|
||||
]).then(() => this.networkManager.reconcile(config));
|
||||
this.lifecycleTail = this.networkReady.then(
|
||||
() => undefined,
|
||||
() => undefined,
|
||||
@@ -400,9 +406,9 @@ export class OneBotInstance {
|
||||
// reconcile and every transport have actually closed.
|
||||
const closeErrors: unknown[] = [];
|
||||
const stores = [
|
||||
['message', () => this.messageStore.close()],
|
||||
['media', () => this.mediaStore.close()],
|
||||
['reaction', () => this.reactionStore.close()],
|
||||
['message', async () => await this.messageStore.close()],
|
||||
['media', async () => await this.mediaStore.close()],
|
||||
['reaction', async () => await this.reactionStore.close()],
|
||||
] as const;
|
||||
|
||||
for (const [name, close] of stores) {
|
||||
@@ -411,7 +417,7 @@ export class OneBotInstance {
|
||||
}
|
||||
|
||||
try {
|
||||
close();
|
||||
await close();
|
||||
this.storeCloseState[name] = true;
|
||||
} catch (error) {
|
||||
closeErrors.push(error);
|
||||
@@ -466,9 +472,12 @@ export class OneBotInstance {
|
||||
source: 'bridge' | 'send' = 'bridge',
|
||||
startedAt = Date.now(),
|
||||
): Promise<void> {
|
||||
if (source === 'bridge') {
|
||||
this.cacheMessageEvent(event);
|
||||
if (!(await this.cacheMessageEvent(event))) {
|
||||
this.traceEventHandoffTerminal(event, startedAt, 'suppressed', 'private_recall');
|
||||
return;
|
||||
}
|
||||
|
||||
if (source === 'bridge') {
|
||||
if (this.consumePendingSelfSentEcho(event)) {
|
||||
this.log.trace(
|
||||
'duplicate self-message echo suppressed message_id=%d message_seq=%d',
|
||||
@@ -484,7 +493,6 @@ export class OneBotInstance {
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
this.cacheMessageEvent(event);
|
||||
this.rememberPendingSelfSentEcho(event);
|
||||
}
|
||||
|
||||
@@ -492,7 +500,7 @@ export class OneBotInstance {
|
||||
// synthetic event here would produce a second, less accurate "received"
|
||||
// line whose private user_id points at the bot itself.
|
||||
if (source === 'bridge') {
|
||||
this.logReceivedMessage(event);
|
||||
await this.logReceivedMessage(event);
|
||||
}
|
||||
|
||||
// Built-in `#sl` only consumes events observed from QQ. Action-originated
|
||||
@@ -518,7 +526,7 @@ export class OneBotInstance {
|
||||
event: JsonObject,
|
||||
startedAt: number,
|
||||
outcome: 'suppressed' | 'reporting_started',
|
||||
reason?: 'duplicate_self_echo' | 'status_command',
|
||||
reason?: 'duplicate_self_echo' | 'status_command' | 'private_recall',
|
||||
): void {
|
||||
this.log.trace(() => [
|
||||
'event_terminal post_type=%s outcome=%s%s ms=%d',
|
||||
@@ -702,7 +710,7 @@ export class OneBotInstance {
|
||||
return rejected;
|
||||
}
|
||||
|
||||
private logReceivedMessage(event: JsonObject): void {
|
||||
private async logReceivedMessage(event: JsonObject): Promise<void> {
|
||||
const isSelf = event.post_type === 'message_sent';
|
||||
|
||||
if (event.post_type !== 'message' && !isSelf) {
|
||||
@@ -738,7 +746,7 @@ export class OneBotInstance {
|
||||
|
||||
if (type === 'reply') {
|
||||
const replyId = toInt(data.id);
|
||||
renderedParts.push(formatReply(this.messageStore, identity, replyId));
|
||||
renderedParts.push(await formatReply(this.messageStore, identity, replyId));
|
||||
} else {
|
||||
renderedParts.push(formatMessageSegments([seg as JsonValue]));
|
||||
}
|
||||
@@ -781,15 +789,15 @@ export class OneBotInstance {
|
||||
}
|
||||
}
|
||||
|
||||
private cacheMessageEvent(event: JsonObject): void {
|
||||
private async cacheMessageEvent(event: JsonObject): Promise<boolean> {
|
||||
if (event.post_type !== 'message' && event.post_type !== 'message_sent') {
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
|
||||
const messageId = toInt(event.message_id);
|
||||
|
||||
if (messageId === 0) {
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
|
||||
const isGroup = event.message_type === 'group';
|
||||
@@ -806,18 +814,25 @@ export class OneBotInstance {
|
||||
: PRIVATE_MESSAGE_EVENT;
|
||||
|
||||
if (sessionId === 0) {
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
|
||||
this.messageStore.storeEvent(messageId, isGroup, sessionId, sequence, eventName, event);
|
||||
return this.messageStore.storeEvent(
|
||||
messageId,
|
||||
isGroup,
|
||||
sessionId,
|
||||
sequence,
|
||||
eventName,
|
||||
event,
|
||||
);
|
||||
}
|
||||
|
||||
private cacheMessageMeta(messageId: number, meta: MessageMeta): void {
|
||||
private async cacheMessageMeta(messageId: number, meta: MessageMeta): Promise<void> {
|
||||
if (!Number.isInteger(messageId) || messageId === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.messageStore.storeMeta(messageId, meta);
|
||||
await this.messageStore.storeMeta(messageId, meta);
|
||||
}
|
||||
|
||||
private makeLifecycleEvent(subType: 'connect' | 'enable' | 'disable'): JsonObject {
|
||||
|
||||
Reference in new issue
Block a user