diff --git a/backend/internal/service/ratelimit_cn_frequency_429_test.go b/backend/internal/service/ratelimit_cn_frequency_429_test.go new file mode 100644 index 0000000000..2343a0152c --- /dev/null +++ b/backend/internal/service/ratelimit_cn_frequency_429_test.go @@ -0,0 +1,139 @@ +//go:build unit + +package service + +import ( + "context" + "net/http" + "testing" + "time" + + "github.com/Wei-Shaw/sub2api/internal/config" + "github.com/stretchr/testify/require" +) + +// 智谱 GLM Coding Plan 实测 429 文案:额度充裕时的「限频」。 +const zhipuFrequencyLimitedBody = `{"error":{"code":"1302","message":"您的账户已达到速率限制,请您控制请求频率"}}` + +// newZhipuCodingPlanAccount 构造一个智谱 Coding Plan 账号,Extra 带 5h/weekly +// 用量与重置快照(与 CNProviderQuotaService 写入的键一致)。 +func newZhipuCodingPlanAccount(h5Used, weeklyUsed float64, resetIn time.Duration) *Account { + extra := map[string]any{ + cnExtraKey(PlatformZhipu, cnExtraSuffix5hUsed): h5Used, + cnExtraKey(PlatformZhipu, cnExtraSuffixWeeklyUsed): weeklyUsed, + } + if resetIn > 0 { + extra[cnExtraKey(PlatformZhipu, cnExtraSuffix5hReset)] = time.Now().Add(resetIn).UTC().Format(time.RFC3339) + extra[cnExtraKey(PlatformZhipu, cnExtraSuffixWeeklyReset)] = time.Now().Add(4 * 24 * time.Hour).UTC().Format(time.RFC3339) + } + return &Account{ + ID: 701, + Platform: PlatformZhipu, + Type: AccountTypeAPIKey, + Credentials: map[string]any{"account_mode": AccountModeCoding}, + Extra: extra, + } +} + +func runZhipu429(service *RateLimitService, account *Account, body string) bool { + return service.HandleUpstreamError( + context.Background(), + account, + http.StatusTooManyRequests, + http.Header{}, + []byte(body), + ) +} + +// 额度充裕 + 限频文案 → 走秒级 429 兜底,而不是冷却到 5h 窗口结束。 +func TestHandleUpstreamError_ZhipuFrequency429UsesShortCooldown(t *testing.T) { + repo := &rateLimitAccountRepoStub{} + service := NewRateLimitService(repo, nil, &config.Config{}, nil, nil) + account := newZhipuCodingPlanAccount(12, 4, 4*time.Hour) + + runZhipu429(service, account, zhipuFrequencyLimitedBody) + + require.Equal(t, 0, repo.setErrorCalls, "a frequency 429 must never SetError") + require.Equal(t, 1, repo.rateLimitedCalls) + require.Equal(t, account.ID, repo.lastRateLimitedID) + require.WithinDuration(t, time.Now().Add(defaultRateLimit429CooldownSeconds*time.Second), + repo.lastRateLimitedAt, 30*time.Second, + "frequency 429 must cool down seconds, not to the 5h window reset") +} + +// 快照显示窗口已耗尽时,即使文案是限频,也仍然冷却到窗口重置点(护栏)。 +func TestHandleUpstreamError_ZhipuFrequency429WithExhaustedSnapshotKeepsWindowCooldown(t *testing.T) { + repo := &rateLimitAccountRepoStub{} + service := NewRateLimitService(repo, nil, &config.Config{}, nil, nil) + account := newZhipuCodingPlanAccount(100, 40, 2*time.Hour) + + runZhipu429(service, account, zhipuFrequencyLimitedBody) + + require.Equal(t, 0, repo.setErrorCalls) + require.Equal(t, 1, repo.rateLimitedCalls) + require.True(t, repo.lastRateLimitedAt.After(time.Now().Add(time.Hour)), + "exhausted snapshot must keep cooling down to the window reset") +} + +// 额度类文案(即使同时出现"速率限制"字样)不降级,仍按窗口冷却。 +func TestHandleUpstreamError_ZhipuQuotaWorded429KeepsWindowCooldown(t *testing.T) { + repo := &rateLimitAccountRepoStub{} + service := NewRateLimitService(repo, nil, &config.Config{}, nil, nil) + account := newZhipuCodingPlanAccount(12, 4, 2*time.Hour) + body := `{"error":{"message":"已达速率限制:本月额度已用尽,请升级套餐"}}` + + runZhipu429(service, account, body) + + require.Equal(t, 1, repo.rateLimitedCalls) + require.True(t, repo.lastRateLimitedAt.After(time.Now().Add(time.Hour)), + "quota wording must not be downgraded to the short 429 cooldown") +} + +// 余额不足文案仍走原有的临时停调路径,不受本次改动影响。 +func TestHandleUpstreamError_ZhipuInsufficientBalance429StillTempUnschedulable(t *testing.T) { + repo := &rateLimitAccountRepoStub{} + service := NewRateLimitService(repo, nil, &config.Config{}, nil, nil) + account := newZhipuCodingPlanAccount(12, 4, 4*time.Hour) + + runZhipu429(service, account, `{"error":{"message":"余额不足,请充值"}}`) + + require.Equal(t, 0, repo.rateLimitedCalls) + require.Equal(t, 1, repo.tempCalls) + require.Contains(t, repo.lastTempReason, cnBalanceLowReasonPrefix) +} + +func TestCNProviderResponseIndicatesFrequencyLimit(t *testing.T) { + cases := []struct { + name string + body string + want bool + }{ + {"zhipu frequency wording", zhipuFrequencyLimitedBody, true}, + {"generic too many requests", `{"error":{"message":"Too many requests, please slow down"}}`, true}, + {"openai style rate limit", `{"error":{"code":"rate_limit_exceeded","message":"Rate limit exceeded"}}`, true}, + {"quota wording wins", `{"error":{"message":"已达到速率限制,但本月额度已用尽"}}`, false}, + {"usage limit wording wins", `{"error":{"message":"You've reached your weekly usage limit"}}`, false}, + {"insufficient balance wins", `{"error":{"message":"insufficient balance"}}`, false}, + {"unrelated error", `{"error":{"message":"model not found"}}`, false}, + {"empty body", ``, false}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + require.Equal(t, tc.want, cnProviderResponseIndicatesFrequencyLimit([]byte(tc.body))) + }) + } +} + +func TestCNProviderQuotaSnapshotNearExhausted(t *testing.T) { + require.False(t, cnProviderQuotaSnapshotNearExhausted( + newZhipuCodingPlanAccount(12, 4, time.Hour), cnFrequencyLimitQuotaGuardPercent)) + require.True(t, cnProviderQuotaSnapshotNearExhausted( + newZhipuCodingPlanAccount(90, 4, time.Hour), cnFrequencyLimitQuotaGuardPercent)) + require.True(t, cnProviderQuotaSnapshotNearExhausted( + newZhipuCodingPlanAccount(12, 95, time.Hour), cnFrequencyLimitQuotaGuardPercent)) + + // 快照缺失:无法证明与额度无关 → 保守视为接近耗尽(沿用窗口冷却)。 + require.True(t, cnProviderQuotaSnapshotNearExhausted( + &Account{ID: 702, Platform: PlatformZhipu}, cnFrequencyLimitQuotaGuardPercent)) + require.True(t, cnProviderQuotaSnapshotNearExhausted(nil, cnFrequencyLimitQuotaGuardPercent)) +} diff --git a/backend/internal/service/ratelimit_cn_providers.go b/backend/internal/service/ratelimit_cn_providers.go index 226f7bdb0f..8ffa3e774d 100644 --- a/backend/internal/service/ratelimit_cn_providers.go +++ b/backend/internal/service/ratelimit_cn_providers.go @@ -121,6 +121,90 @@ func cnProviderResponseIndicatesInsufficientBalance(body []byte) bool { strings.Contains(s, "no enough balance") } +// cnFrequencyLimitQuotaGuardPercent 是「频率型 429」降级前必须通过的额度护栏: +// 快照里任一窗口用量达到该百分比,就不再按频控处理,仍冷却到窗口重置点。 +// 快照最多滞后一个额度探测周期(默认 10 分钟),因此真实耗尽在极端情况下可能 +// 被短暂降级成秒级冷却;5 秒后重试仍会拿到 429,下一轮探测刷新快照后自动纠正。 +const cnFrequencyLimitQuotaGuardPercent = 90 + +// cnFrequencyLimitMarkers 是「请求频率/速率限制」类 429 的文案特征(小写比较)。 +// 智谱实测文案:「您的账户已达到速率限制,请您控制请求频率」。 +var cnFrequencyLimitMarkers = []string{ + "速率限制", + "请求频率", + "访问频率", + "请求过于频繁", + "请求太频繁", + "too many requests", + "rate limit exceeded", + "rate_limit_exceeded", + "ratelimit exceeded", +} + +// cnQuotaExhaustionMarkers 是额度耗尽类文案;命中任一即认为 429 与额度有关, +// 绝不降级为短冷却(宁可多停调,也不要把真耗尽当成限频反复重试)。 +var cnQuotaExhaustionMarkers = []string{ + "额度", + "已用尽", + "用完", + "quota", + "usage limit", + "insufficient", + "balance", +} + +// cnProviderResponseIndicatesFrequencyLimit 通过响应体文案识别「请求频率/速率 +// 限制」类 429 —— 它与滚动窗口的额度消耗无关,重试即可能成功,不该按额度 +// 耗尽冷却到窗口结束。命中频控文案且不含任何额度类文案才算频控。 +func cnProviderResponseIndicatesFrequencyLimit(body []byte) bool { + if len(body) == 0 { + return false + } + s := strings.ToLower(string(body)) + matched := false + for _, marker := range cnFrequencyLimitMarkers { + if strings.Contains(s, marker) { + matched = true + break + } + } + if !matched { + return false + } + for _, marker := range cnQuotaExhaustionMarkers { + if strings.Contains(s, marker) { + return false + } + } + return true +} + +// cnProviderQuotaSnapshotNearExhausted 判断 Coding Plan 账号的滚动窗口用量快照 +// 是否已接近耗尽(任一窗口 >= percent)。快照完全缺失(额度探测还没跑过或 +// 解析失败)时返回 true:此时无法证明 429 与额度无关,保守沿用窗口冷却。 +func cnProviderQuotaSnapshotNearExhausted(account *Account, percent float64) bool { + if account == nil || len(account.Extra) == 0 { + return true + } + provider := account.Platform + suffixes := []string{cnExtraSuffix5hUsed, cnExtraSuffixWeeklyUsed} + if cnQuotaHasMonthlyWindow(provider) { + suffixes = append(suffixes, cnExtraSuffixMonthlyUsed) + } + seen := false + for _, suffix := range suffixes { + raw, ok := account.Extra[cnExtraKey(provider, suffix)] + if !ok { + continue + } + seen = true + if schedulingPercentValue(raw) >= percent { + return true + } + } + return !seen +} + // handleCNProviderInsufficientBalance 把余额不足标记为可恢复的临时停调: // 写入 balance_low 快照 + SetTempUnschedulable 一个余额检测周期, // 由周期任务在余额恢复后清除。返回前已通知调度阻塞。 @@ -288,7 +372,22 @@ func (s *RateLimitService) applyCNProviderReactive429( s.handleCNProviderInsufficientBalance(ctx, account, extractUpstreamErrorMessage(responseBody)) return true } - // 2) Coding Plan 窗口耗尽:冷却到快照中最早的窗口重置点(见 + // 2) 频率型 429("速率限制"/"请求频率"):与滚动窗口额度无关,交给默认 429 + // 兜底使用秒级冷却(管理端可配,默认 5s)。否则一次并发突发会把账号冷却到 + // 5h 窗口结束(实测 1.5~4.8 小时),把「限频」误伤成「额度耗尽」。 + // 护栏:快照显示窗口用量已接近耗尽时不降级,仍按窗口冷却(见 + // cnProviderQuotaSnapshotNearExhausted)。 + if account.IsCodingPlan() && + cnProviderResponseIndicatesFrequencyLimit(responseBody) && + !cnProviderQuotaSnapshotNearExhausted(account, cnFrequencyLimitQuotaGuardPercent) { + slog.Info("cn_coding_plan_frequency_429", + "account_id", account.ID, + "platform", account.Platform, + "handling", "short_429_cooldown", + ) + return false + } + // 3) Coding Plan 窗口耗尽:冷却到快照中最早的窗口重置点(见 // cnProviderQuotaSnapshotReset:429 多由 5h 窗口触发,取较早点避免过度停调)。 if account.IsCodingPlan() && s.cooldownCNProviderToQuotaSnapshotReset(ctx, account, "429", "cn_coding_plan_rate_limited") != nil {