perf: 重构退群监控,持久化快照并降低 CPU 占用

- 持久化群成员
- 支持 TraceMemo 重启后恢复离线期间的退群检测
- 使用 Batch Membership 替代逐群成员查询
- 降低大量群聊监控时的 CPU 和数据库查询开销
This commit is contained in:
电摇小子 committed 2026-09-03 01:50:34 +08:00
1 parent 4d758b2aca
commit 3462d49516
6 files changed
+1284 -202

No files matched your search

+100
View File
@@ -59,6 +59,46 @@ export interface Wcdb4GroupMember {
m_nsHeadImgUrl: string
}
export type Wcdb4GroupMemberBatchStatus = 'ok' | 'not_found'
export interface Wcdb4GroupMemberIdsBatchItem {
roomId: string
status: Wcdb4GroupMemberBatchStatus
memberWxids: string[]
}
function normalizeGroupMemberIdsBatch(
value: unknown,
requestedRoomIds: string[]
): Wcdb4GroupMemberIdsBatchItem[] | null {
if (!Array.isArray(value) || value.length !== requestedRoomIds.length) return null
const requested = new Set(requestedRoomIds)
const results = new Map<string, Wcdb4GroupMemberIdsBatchItem>()
for (const rawItem of value) {
if (!rawItem || typeof rawItem !== 'object') return null
const item = rawItem as Record<string, unknown>
const roomId = typeof item.roomId === 'string' ? item.roomId.trim() : ''
const status = item.status
if (
!roomId ||
!requested.has(roomId) ||
results.has(roomId) ||
(status !== 'ok' && status !== 'not_found') ||
!Array.isArray(item.memberWxids) ||
item.memberWxids.some((memberWxid) => typeof memberWxid !== 'string' || !memberWxid.trim())
) {
return null
}
results.set(roomId, {
roomId,
status,
memberWxids: Array.from(new Set(item.memberWxids.map((memberWxid) => memberWxid.trim())))
})
}
if (results.size !== requestedRoomIds.length) return null
return requestedRoomIds.map((roomId) => results.get(roomId)!)
}
/**
* 联系人表中独立保存的成员名称字段。
*
@@ -395,6 +435,9 @@ export class Wcdb4Client {
private wcdbGetGroupMembers:
| ((handle: number, chatroomId: string, outJson: WcdbVoidOut) => number)
| null = null
private wcdbGetGroupMembersBatch:
| ((handle: number, chatroomIdsJson: string, outJson: WcdbVoidOut) => number)
| null = null
private wcdbGetGroupNicknames:
| ((handle: number, chatroomId: string, outJson: WcdbVoidOut) => number)
| null = null
@@ -1900,6 +1943,55 @@ export class Wcdb4Client {
}
}
/**
* 退群检测热路径:只读取原始成员行并提取 wxid,不补群昵称、联系人资料或头像。
* null 表示读取失败,避免调用方把失败误判为成员列表为空。
*/
async getGroupMemberIdsAsync(chatroomId: string): Promise<string[] | null> {
if (!this.wcdbGetGroupMembers || !chatroomId) return null
try {
const rows = await this.callJsonAsync<Record<string, unknown>[]>(
this.wcdbGetGroupMembers as unknown as KoffiAsyncFunction,
chatroomId
)
const memberRows = Array.isArray(rows) ? rows : []
return Array.from(new Set(this.groupMemberUsernames(memberRows)))
} catch (error) {
console.warn(`[WCDB4] async group member ids failed chatroom=${chatroomId}:`, error)
return null
}
}
isGroupMemberIdsBatchAvailable(): boolean {
return Boolean(this.wcdbGetGroupMembersBatch)
}
async getGroupMemberIdsBatchAsync(
roomIds: string[]
): Promise<Wcdb4GroupMemberIdsBatchItem[] | null> {
const requestedRoomIds = Array.from(
new Set(
(Array.isArray(roomIds) ? roomIds : [])
.map((roomId) => String(roomId || '').trim())
.filter(Boolean)
)
)
if (requestedRoomIds.length === 0) return []
if (!this.wcdbGetGroupMembersBatch) return null
try {
const raw = await this.callJsonAsync<unknown>(
this.wcdbGetGroupMembersBatch as unknown as KoffiAsyncFunction,
JSON.stringify(requestedRoomIds)
)
return normalizeGroupMemberIdsBatch(raw, requestedRoomIds)
} catch (error) {
console.warn('[WCDB4] async group member ids batch failed:', error)
return null
}
}
/**
* 将群成员接口与联系人表组装为语义明确的成员快照。
*
@@ -2390,6 +2482,14 @@ export class Wcdb4Client {
this.wcdbGetGroupMembers = null
}
try {
this.wcdbGetGroupMembersBatch = lib.func(
'int32 wcdb_get_group_members_batch(int64 handle, const char* chatroomIdsJson, _Out_ void** outJson)'
) as (handle: number, chatroomIdsJson: string, outJson: WcdbVoidOut) => number
} catch {
this.wcdbGetGroupMembersBatch = null
}
try {
this.wcdbGetGroupNicknames = lib.func(
'int32 wcdb_get_group_nicknames(int64 handle, const char* chatroomId, _Out_ void** outJson)'