From 92fa3b89eb40353b1b83a037f45b54b6efdac642 Mon Sep 17 00:00:00 2001 From: sliverp Date: Fri, 30 Jan 2026 11:21:29 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E5=BA=8F=E5=8F=B7=E7=94=9F=E6=88=90=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E4=BB=A5=E7=A1=AE=E4=BF=9D=E5=94=AF=E4=B8=80=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/api.ts b/src/api.ts index b5d7a17..563c471 100644 --- a/src/api.ts +++ b/src/api.ts @@ -56,18 +56,21 @@ export function clearTokenCache(): void { /** * msg_seq 追踪器 - 用于对同一条消息的多次回复 * key: msg_id, value: 当前 seq 值 + * 使用时间戳作为基础值,确保进程重启后不会重复 */ const msgSeqTracker = new Map(); +const seqBaseTime = Math.floor(Date.now() / 1000) % 100000000; // 取秒级时间戳的后8位作为基础 /** * 获取并递增消息序号 + * 返回的 seq 会基于时间戳,避免进程重启后重复 */ export function getNextMsgSeq(msgId: string): number { const current = msgSeqTracker.get(msgId) ?? 0; const next = current + 1; msgSeqTracker.set(msgId, next); - // 清理过期的序号(超过 5 次或 60 分钟后无意义) + // 清理过期的序号 // 简单策略:保留最近 1000 条 if (msgSeqTracker.size > 1000) { const keys = Array.from(msgSeqTracker.keys()); @@ -76,7 +79,8 @@ export function getNextMsgSeq(msgId: string): number { } } - return next; + // 结合时间戳基础值,确保唯一性 + return seqBaseTime + next; } /**