refactor(common,core): extract mapWithConcurrency util; use in forward enrich

阶段 2:把「限量+并发分批」的手写循环收进一个 common util。此形状已被复制进
私有 SLE(friend.ts),是已发生的复制漂移;抽出后堵住它、并给未来的富化点一个
现成原语(cap + 并发批 + best-effort 是防 Tencent 风控封号的标准配方)。

- 新增 common/concurrency.ts:mapWithConcurrency(items, concurrency, fn),批处理
  语义(每批 Promise.all 等齐再下一批,非 rolling pool,保持 forward 现有行为),
  结果按输入顺序返回。7 个单测覆盖顺序/绝对index/峰值≤limit+跨批serialize/
  不整除/单批/空/concurrency<1。
- 迁移 forward.ts residue 昵称富化的并发批循环;cap(slice MAX)/dedup(Set)/
  best-effort(fn 内 try/catch) 保留在 callsite。行为严格不变。

对抗性 Agent 逐条对比 HEAD 确认等价。common 50 / core 467 全绿、typecheck 干净。
This commit is contained in:
DrSmoothl committed 2026-07-01 17:29:25 +08:00
1 parent 9d67630396
commit b6cde5101d
4 files changed
+104 -6

No files matched your search

+5 -6
View File
@@ -1,4 +1,5 @@
import type { PacketInfo } from '@snowluma/common/protocol-types';
import { mapWithConcurrency } from '@snowluma/common/concurrency';
import type {
LongMsgResult,
RecvLongMsgReq,
@@ -676,14 +677,12 @@ export class ForwardApi {
.slice(0, FORWARD_PROFILE_MAX);
if (uins.length > 0) {
const byUin = new Map<number, string>();
for (let i = 0; i < uins.length; i += FORWARD_PROFILE_CONCURRENCY) {
const batch = uins.slice(i, i + FORWARD_PROFILE_CONCURRENCY);
const results = await Promise.all(batch.map(async (uin): Promise<readonly [number, string]> => {
const results = await mapWithConcurrency(uins, FORWARD_PROFILE_CONCURRENCY,
async (uin): Promise<readonly [number, string]> => {
try { return [uin, (await this.ctx.apis.contacts.fetchUserProfile(uin)).nickname] as const; }
catch { return [uin, ''] as const; }
}));
for (const [uin, nick] of results) byUin.set(uin, nick);
}
});
for (const [uin, nick] of results) byUin.set(uin, nick);
for (const n of residue) {
if (!this.needsName(n)) continue;
const nick = byUin.get(n.userUin);