oms/backend/test/app/lib/dgjRateLimit.test.js
liufumingren 9e1c476a55 perf(dgj): 平台请求节流 + 限流退避重试
线上取号/发货遇到服务商级限流:
`isv.request-limited 请求太频繁…配置限流QPS:600, 当前访问QPS:687`。
QPS 额度是整个 ISV 共享的,单靠降我们的并发压不住,所以两手都做:
削自己峰值 + 被限流时退避重试而不是直接判失败。

- dgj.request():所有平台调用的唯一出口,统一排队,相邻请求至少间隔
  DGJ_REQUEST_INTERVAL_MS(默认 100ms,单进程约 10 请求/秒)
- postPlatformForm():命中 isv.request-limited / 请求太频繁 / 访问QPS 时
  指数退避重试(DGJ_RATE_LIMIT_RETRY 默认 3 次、DGJ_RATE_LIMIT_BACKOFF_MS 默认 1000ms),
  仍限流则抛「店管家接口限流:…已自动退避重试 N 次仍被限流,请稍后重试」并保留原始返回
- waybillGet:批量取号并发 4 → 2(WAYBILL_CONCURRENCY 可调),
  每单 3 次平台调用(回读订单 + 模板校验 + ExpressPrint),并发越大突发越猛
- externalSendSync.recordFailure:平台限流不算这单的错,
  保持「待重试」且不累加 external_send_times,避免批量重试时一批单被打成「异常」
- 单测 dgjRateLimit.test.js(4 例):限流识别、退避后成功、重试耗尽报错、全局间隔生效
- 文档:请求量控制一节补 QPS 限流口径与全部节流开关

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-07 09:46:45 +08:00

116 lines
4.6 KiB
JavaScript
Raw Permalink 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.

'use strict';
const { assert } = require('egg-mock/bootstrap');
const DgjService = require('../../../app/lib/printPlatform/dgj');
/** 线上真实返回(isv 服务商级限流) */
const RATE_LIMIT_JSON = {
Success: false,
ErrorCode: '500',
Message: 'isv.request-limited 请求太频繁,请稍后重试,详细信息:时间:2026-11-07 09:36:08, 配置限流QPS:600, 当前访问QPS:687',
};
const SUCCESS_JSON = { Success: true, ErrorCode: '200', Data: { ok: 1 } };
/**
* 造一个只用账号信息的服务实例(不落库)
* @return {Object} 店管家服务实例
*/
function buildService() {
return new DgjService({
platform: { code: 'dgj', name: '店管家', url: 'fxali.dgjapp.com', fetch_waybill_no: 1 },
account: { id: 1, account: 'tester', password: 'pwd', token: 'token', cookie: '' },
});
}
/**
* 记录每次平台调用的时刻
* @param {Array<Object>} responses 依次返回的响应
* @return {{calls: Array<number>, stub: Function}} 调用时刻与桩函数
*/
function stubRequest(responses) {
const calls = [];
const stub = async () => {
calls.push(Date.now());
const next = responses.length > 1 ? responses.shift() : responses[0];
return { status: 200, json: next, text: JSON.stringify(next), setCookies: [] };
};
return { calls, stub };
}
describe('lib/printPlatform/dgj 限流识别与退避重试', () => {
const originBackoff = process.env.DGJ_RATE_LIMIT_BACKOFF_MS;
const originRetry = process.env.DGJ_RATE_LIMIT_RETRY;
beforeEach(() => {
process.env.DGJ_RATE_LIMIT_BACKOFF_MS = '5';
process.env.DGJ_RATE_LIMIT_RETRY = '2';
process.env.DGJ_REQUEST_INTERVAL_MS = '0';
});
afterEach(() => {
if (originBackoff === undefined) delete process.env.DGJ_RATE_LIMIT_BACKOFF_MS;
else process.env.DGJ_RATE_LIMIT_BACKOFF_MS = originBackoff;
if (originRetry === undefined) delete process.env.DGJ_RATE_LIMIT_RETRY;
else process.env.DGJ_RATE_LIMIT_RETRY = originRetry;
delete process.env.DGJ_REQUEST_INTERVAL_MS;
});
it('识别服务商级限流返回', () => {
assert.strictEqual(DgjService.isRateLimited(RATE_LIMIT_JSON), true);
assert.strictEqual(DgjService.isRateLimited({ Message: '订单不是待发货状态' }), false);
assert.strictEqual(DgjService.isRateLimited(null), false);
});
it('限流后自动退避重试,成功则正常返回', async () => {
const service = buildService();
const { calls, stub } = stubRequest([ RATE_LIMIT_JSON, RATE_LIMIT_JSON, SUCCESS_JSON ]);
service.request = stub;
const res = await service.postPlatformForm('fxali.dgjapp.com', '/NewOrder/OnlineSend', new URLSearchParams('a=1'), 'trace', 'db');
console.log('重试后成功:调用次数 =', calls.length, '| Success =', res.json.Success);
assert.strictEqual(calls.length, 3, '两次限流 + 一次成功');
assert.strictEqual(res.json.Success, true);
});
it('重试仍被限流时抛出可读错误', async () => {
const service = buildService();
const { calls, stub } = stubRequest([ RATE_LIMIT_JSON ]);
service.request = stub;
let error = null;
try {
await service.postPlatformForm('fxali.dgjapp.com', '/TemplateSet/CheckTemplateAvaliable', new URLSearchParams('a=1'), 'trace', 'db');
} catch (err) {
error = err;
}
console.log('限流失败:调用次数 =', calls.length, '| 错误 =', error && error.message);
assert.ok(error, '应当抛错');
assert.match(error.message, /店管家接口限流/);
assert.match(error.message, /请稍后重试/);
assert.strictEqual(calls.length, 3, '首次 + 2 次重试');
assert.ok(error.rawResponse && error.rawResponse.Message, '保留平台原始返回便于留痕');
});
it('全局请求间隔生效(请求按 DGJ_REQUEST_INTERVAL_MS 排队)', async () => {
process.env.DGJ_REQUEST_INTERVAL_MS = '40';
const service = buildService();
const originFetch = global.fetch;
const stamps = [];
global.fetch = async () => {
stamps.push(Date.now());
return { status: 200, text: async () => '{}', headers: { getSetCookie: () => [] } };
};
try {
await service.request('https://fxali.dgjapp.com/a');
await service.request('https://fxali.dgjapp.com/b');
await service.request('https://fxali.dgjapp.com/c');
} finally {
global.fetch = originFetch;
}
const gaps = stamps.slice(1).map((time, index) => time - stamps[index]);
console.log('相邻请求间隔(ms):', gaps.join(', '));
assert.strictEqual(stamps.length, 3);
assert.ok(gaps.every(gap => gap >= 30), '每次请求至少间隔 40ms(留少量误差)');
});
});