perf(external-send): 控制回传平台的请求量,避免被判刷单

全部回读订单行在批量场景会被平台当成高频访问,补齐三处节流:

- syncPending:同批相邻两单之间留 EXTERNAL_SEND_INTERVAL_MS(默认 300ms)
- resolveTraceId:按账号缓存,批量内只调一次 getUserInfo(原来每单一次)
- dgj.resolveTraceId():实例级缓存 5 分钟的 traceId,
  取号(fetchWaybillNo)与重打(waybillPrintData)改用它,批量取号不再逐单 getUserInfo

配合上一版的「快照拼报文」,发货每单只有 1 次 POST,不再回读订单行。

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
liufumingren 2026-11-07 08:14:12 +08:00
parent bcca0075be
commit 498543b9d7
4 changed files with 68 additions and 12 deletions

View File

@ -2394,13 +2394,7 @@ class DgjService extends PrintPlatformService {
const originalPlatformType = originalCode ? DgjService.resolvePlatformType(originalCode) : '';
const host = resolveOrderHost(originalPlatformType || template.platform_type);
let traceId = '';
try {
const info = await this.getUserInfo();
traceId = (info.data && info.data.TraceId) || '';
} catch {
// 忽略:traceId 缺失不阻断取号(与模板查询同口径)
}
const traceId = await this.resolveTraceId();
// 分区:模板记录的分区优先,订单不一定同区(模板按账号同步、店铺可能迁区),命中会改写
let dbname = String(template.dbname || '').trim();
@ -2613,6 +2607,29 @@ class DgjService extends PrintPlatformService {
return Array.isArray(data.Rows) ? data.Rows : [];
}
/**
* 取登录上下文 traceId(实例级缓存)
*
* `getUserInfo` 也是一次平台请求,批量取号/重打时逐单调用会把请求量翻几倍,
* 容易被平台判成高频访问;同一实例内缓存 5 分钟足够(traceId 只用于串联日志)。
* @param {Object} [options] 参数
* @param {number} [options.ttlMs] 缓存时长(毫秒)
* @return {Promise<string>} traceId,取不到为空串
*/
async resolveTraceId({ ttlMs = 5 * 60 * 1000 } = {}) {
const now = Date.now();
if (this._traceCache && now - this._traceCache.at < ttlMs) return this._traceCache.value;
let value = '';
try {
const info = await this.getUserInfo();
value = (info.data && info.data.TraceId) || '';
} catch {
value = '';
}
this._traceCache = { value, at: now };
return value;
}
/**
* 取号前校验模板可用性与面单余额(只读,与旧系统 TemplateSet/CheckTemplateAvaliable 同口径)
* @param {string} host 平台域名

View File

@ -349,8 +349,9 @@ async function fetchGroup(ctx, { principalId, memo, group }) {
let traceId = '';
try {
const info = await service.getUserInfo();
traceId = (info.data && info.data.TraceId) || '';
traceId = typeof service.resolveTraceId === 'function'
? await service.resolveTraceId()
: ((await service.getUserInfo()).data || {}).TraceId || '';
} catch {
traceId = '';
}

View File

@ -22,6 +22,23 @@ const SEND_STATUS_LABELS = {
/** 失败重试上限:达到后流转为「异常」,等人工批量重试 */
const MAX_SEND_TIMES = 3;
/**
* 同批逐单同步的间隔(毫秒)
*
* 店管家对高频请求会判成刷单/攻击,批量回传时逐单之间留一点间隔;
* 现在发货不再回读订单行(用本地快照拼报文),每单只有 1 次 POST。
*/
const SEND_INTERVAL_MS = Number(process.env.EXTERNAL_SEND_INTERVAL_MS || 300);
/**
* 简单等待
* @param {number} ms 毫秒
* @return {Promise<void>} 无返回值
*/
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
/**
* 截断字符串,避免超出表字段长度
* @param {*} value 原始值
@ -301,16 +318,25 @@ class ExternalSendSyncService extends Service {
/**
* 取平台登录上下文 traceId(失败不阻断)
*
* 同一次批量同步里按账号缓存:`getUserInfo` 本身也是一次平台请求,
* 逐单重复取等于把请求量翻倍,容易被平台判成高频访问。
* @param {Object} service 平台服务实例
* @return {Promise<string>} traceId
*/
async resolveTraceId(service) {
const key = (service.account && service.account.id) || 'default';
if (!this.traceMemo) this.traceMemo = new Map();
if (this.traceMemo.has(key)) return this.traceMemo.get(key);
let traceId = '';
try {
const info = await service.getUserInfo();
return (info.data && info.data.TraceId) || '';
traceId = (info.data && info.data.TraceId) || '';
} catch (err) {
return '';
traceId = '';
}
this.traceMemo.set(key, traceId);
return traceId;
}
/**
@ -338,7 +364,10 @@ class ExternalSendSyncService extends Service {
let success = 0;
let failed = 0;
let exception = 0;
for (const row of rows) {
for (let index = 0; index < rows.length; index += 1) {
const row = rows[index];
// 平台对高频请求会判成刷单/攻击:同批逐单之间留间隔(可按 SEND_INTERVAL_MS 调整)
if (index > 0) await sleep(SEND_INTERVAL_MS);
const result = await this.syncOrder(row, { trigger: 'schedule' });
if (result.ok) success += 1;
else if (row.external_send_status === SEND_STATUS.EXCEPTION) exception += 1;

View File

@ -85,6 +85,15 @@ OMS 实现:
| `dgj.sendOrders(host, { traceId, dbname, rows, templateId, expressCompanyId, expressCode })` | 按上面口径组装并提交;整包或逐单失败抛「店管家发货同步失败:…」 |
| `service/externalSendSync.js` | 配送后同步的状态机(待同步 → 已同步 / 待重试 → 3 次转异常);快照缺失时才回读平台订单行兜底 |
**请求量控制(避免被平台判成刷单/攻击)**:
| 手段 | 说明 |
|---|---|
| 发货不回读订单行 | 快照拼报文,每单只有 1 次 `POST /NewOrder/OnlineSend` |
| 逐单间隔 | 同批相邻两单之间 `EXTERNAL_SEND_INTERVAL_MS`(默认 300ms) |
| traceId 缓存 | `getUserInfo` 按账号缓存(批量内只调 1 次),不再逐单取 |
| 取号侧同理 | `dgj.resolveTraceId()` 实例级缓存 5 分钟,批量取号不再每单一次 `getUserInfo` |
状态字段(配送表 `order_status_delivery`,所有状态表 + 历史表同构):
| 字段 | 说明 |