Skip to content

fix(quota,web): stop retrying terminal quota recoveries and back off failing statsig fetches - #1110

Open
jun9100 wants to merge 1 commit into
chenyme:mainfrom
jun9100:fix/quota-recovery-and-statsig-backoff
Open

jun9100 wants to merge 1 commit into
chenyme:mainfrom
jun9100:fix/quota-recovery-and-statsig-backoff

Conversation

@jun9100

@jun9100 jun9100 commented Nov 24, 2026

Copy link
Copy Markdown

背景与目标

在排查大规模账号池后台重试与上游高频请求问题时,发现两处与 #1109 相互独立的重试风暴点:

  1. 额度恢复队列对终态账号持续重试:
    • quotarecovery.Service.runOne 在探测返回终态错误(account.ErrNotFound、repository.ErrNotFound、provider.ErrUnauthorized)时未 AckQuotaRecovery,而是继续按退避无限重排;对于携带 RetryAfterDuration() 的临时错误也未尊重更长的 RetryAfter。
    • AccountRepository.ListDueQuotaWindows 与 ListQuotaRecoveryWindows 未关联 provider_accounts 过滤 enabled = true AND auth_status = 'active',导致即使恢复队列 ack 了失效/停用账号,每分钟一次的 reconcileDue 或服务重启仍会将其重新入队。
  2. Statsig 签名拉取缺少失败退避:
    • 当 Grok index/首页暂时不可达或缺少 grok-site-verification 时,statsigSigner.Sign 未记录失败冷却窗口,每个并发后的新请求都会立刻重新抓取上游页面并打印 web_statsig_fetch_failed 警告日志。

本 PR 基于 main 独立提交,与 #1109 无文件冲突,可按任意顺序合并。

主要变更

  • backend/internal/application/quotarecovery/service.go
    • 新增 isTerminalQuotaRecoveryError:对 account.ErrNotFound、repository.ErrNotFound、provider.ErrUnauthorized 直接 AckQuotaRecovery,终止无效重排。
    • 临时错误重排延迟取 max(s.backoff(value.Attempts), provider.ErrorRetryAfter(probeErr)),尊重上游/出口层返回的冷却时间。
  • backend/internal/infra/persistence/relational/account_repository.go
    • ListDueQuotaWindows 与 ListQuotaRecoveryWindows 增加 JOIN provider_accounts,仅返回 enabled = true AND auth_status = 'active' 账号的额度窗口,防止已停用或待重登账号被 reconcileDue / 启动恢复重新入队。
  • backend/internal/infra/provider/web/statsig.go
    • 为 statsigSigner 增加按签名键粒度的指数失败退避(5s -> 10s -> ... -> 2m 上限,带容量上限淘汰),避免上游或签名服务异常时持续高频抓取与告警刷屏。
    • 调用方取消或调用方自身上下文超时(ctx.Err() != nil / context.Canceled)不写入共享失败冷却;成功刷新、Invalidate 与 Clear 时立即清除对应冷却状态。
    • 退避窗口内若存在过期签名,继续返回 stale 签名(标记为 stale_cooldown)并将退避期日志降级为 Debug(web_statsig_fetch_suppressed / web_statsig_using_stale_during_cooldown)。
  • 单元测试:
    • backend/internal/application/quotarecovery/service_test.go
    • backend/internal/infra/persistence/relational/repository_test.go
    • backend/internal/infra/provider/web/statsig_test.go

验证结果

  • gofmt -l:全部修改文件无格式差异
  • go vet ./...:通过
  • go test -race -count=1 ./internal/application/quotarecovery ./internal/infra/persistence/relational ./internal/infra/provider/web ./internal/application/account ./internal/app:全部通过

风险与注意事项

  • 账号重新启用或恢复为 active 状态后,下一次 reconcileDue 周期会自动将其到期窗口重新纳入恢复队列。
  • Statsig 退避仅作用于失败的 (baseURL, signerURL, method, path) 签名键,不影响其他路径;手动触发或防风控触发 Invalidate 时会立即清除退避窗口。

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant