Files
SupplierSystem/server/src/utils/concurrency.ts
T
Ubuntu aa2db57b0f
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
feat(main-system): 免入库单取货链路 + 实收数量「事实登记 + 事后同步」
免入库(主系统骑手上门取货):
- 50 展示「待取货」、副文案「待主系统骑手上门取货」、不建我方配送任务(守卫与开关解耦)
- 我方 50 即回写备货完成;两开关(SKIP_INBOUND_AWAIT_PICKUP / FULFILLMENT_EVENTS)改默认开

核货事实镜像(「核货完毕」标记):
- 按主系统 lookup 的 actual_qty 落台账,新增 GET/POST /orders/:orderCode/main-system-prep-facts[/refresh]
- 详情行内显示「核货完毕」(含核货量),分配对账兜底刷新

实收数量/单位口径变更(对方先改 + 我方事后同步):
- 台账加 applied_at;inbound 与 picking_check 同为事实登记(驳回不回滚)
- 同意才落我方值:免入库改装车核货量、送仓改验收实收(已入库走既有库存调整)、已结算/已结案只留痕
- 计划量与金额一起改,并同步结算投影(syncStatementsForOrderSafe)
- 审核页语义改「主系统实际值(已生效)/ 是否同步」+ 已处理历史(双方时间与落值结论)

台账对照与回写修复:
- 行级对照改整单一次性(同名商品不再重复认领)+ 存量纠正 healOrderPrepMapping
- 堵住客户绑定回填旁路(免入库单不再被派我方骑手)
- 回写触发函数改对象参数(修 branch/order 传反导致的静默不回写)

文档:设计文档 4 篇 +《给主系统 AI 的对接改造清单》§2.9(第六批契约)

回归:服务端全量与 HEAD 基线一致(24 个失败文件同集合);前端 140 文件 / 699 测试全绿;tsc 双端干净
2026-10-09 21:52:17 +08:00

72 lines
2.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 有界并发映射(2026-10-08 规模改造 §6/§7 引入)。
*
* 背景:本系统按「主系统供应商数」放大成 N 个租户,多处循环是「逐租户串行」。
* 500 个分公司时,一次 HTTP 型任务串行跑完要几十秒。这里提供最小可用的
* 「保持结果顺序 + 固定并发度 + 单个失败不炸整轮」的工具。
*
* 约定:
* - 返回数组与入参**同序同长**,第 i 项对应 items[i];
* - 并发度最小 1(=1 即退回串行,用于回滚);
* - `fn` 抛错时该项记 `undefined`,不会中断其余项。**调用方必须自己记日志/计数**
* (可传 `onError`,避免失败只以 undefined 形式静默消失)。
*/
export async function mapWithConcurrency<T, R>(
items: readonly T[],
concurrency: number,
fn: (item: T, index: number) => Promise<R>,
onError?: (error: unknown, item: T, index: number) => void
): Promise<Array<R | undefined>> {
const results: Array<R | undefined> = new Array(items.length);
const limit = Math.max(1, Math.min(Math.floor(concurrency) || 1, items.length || 1));
let cursor = 0;
const workers = new Array(limit).fill(null).map(async () => {
for (;;) {
const index = cursor;
cursor += 1;
if (index >= items.length) return;
try {
results[index] = await fn(items[index], index);
} catch (error) {
results[index] = undefined;
if (onError) {
try {
onError(error, items[index], index);
} catch {
/* 回调自身出错不影响其它项 */
}
} else {
console.error(
`[concurrency] 第 ${index} 项失败(调用方未传 onError,请看调用点是否漏了日志):`,
(error as Error)?.message || error
);
}
}
}
});
await Promise.all(workers);
return results;
}
/**
* 读环境变量里的并发度;非法值退回 fallback,并受两个上限约束:
* `cap`(业务上限)与 `poolMax / 5`(连接池预算)。
*
* 为什么要池预算(规模改造复查):每个并发租户上下文会**独占一条池连接直到任务结束**
* (含回主系统的 HTTP 等待),`PG_POOL_MAX` 默认 30。若运维把池调小(如 10),
* 仍按默认 6 并发就会把池占满、让正常请求排队到 10s 超时。这里按池大小自动收敛。
*/
export function resolveConcurrency(
raw: string | number | undefined,
fallback: number,
cap = 10,
poolMax?: number | string
): number {
const value = Number(raw);
const base =
!Number.isFinite(value) || value < 1 ? Math.max(1, Math.floor(fallback) || 1) : Math.floor(value);
const pool = Number(poolMax);
const poolBudget = Number.isFinite(pool) && pool > 0 ? Math.max(1, Math.floor(pool / 5)) : cap;
return Math.max(1, Math.min(base, cap, poolBudget));
}