Skip to content

feat: 为核心 API 添加重试机制 #3

@luojiyin1987

Description

@luojiyin1987

背景

飞书 API 偶尔返回 429/502/503/504 等瞬时错误,当前实现直接失败,影响用户体验。

改动

sendCardreplyCardpatchMessage 三个核心函数添加指数退避重试:

// 匹配瞬时错误
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
  • 非瞬时错误立即返回

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions