背景
飞书 API 偶尔返回 429/502/503/504 等瞬时错误,当前实现直接失败,影响用户体验。
改动
为 sendCard、replyCard、patchMessage 三个核心函数添加指数退避重试:
// 匹配瞬时错误
const RETRY_PATTERNS = new RegExp(
'rate.?limit|too.?many|service.?unavailable|bad.?gateway|502|503|504',
'i'
)
async function execWithRetry(args: string[], maxRetries = 3) {
for (let attempt = 0; attempt < maxRetries; attempt++) {
const result = await exec(args)
if (result.ok || !RETRY_PATTERNS.test(result.stderr)) return result
await sleep(500 * Math.pow(2, attempt))
}
return exec(args)
}
行为
- 最多重试 3 次
- 指数退避:500ms → 1s → 2s
- 非瞬时错误立即返回
背景
飞书 API 偶尔返回 429/502/503/504 等瞬时错误,当前实现直接失败,影响用户体验。
改动
为
sendCard、replyCard、patchMessage三个核心函数添加指数退避重试:行为