mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-09-24 16:05:44 +08:00
Merge pull request #3809 from hongheshan-svg/fix/upstream-anthropic-429-fallback
fix(ratelimit): Anthropic 无 reset 头的 429 也进入兜底冷却,避免账号永不冷却导致的 429 循环
This commit is contained in:
@@ -97,6 +97,45 @@ func TestHandle429_FallbackDisabledSkipsLocalMark(t *testing.T) {
|
||||
require.Zero(t, accountRepo.rateLimitCalls)
|
||||
}
|
||||
|
||||
// Anthropic 无 reset 头的 429(如 Extra usage required)也应走兜底冷却,
|
||||
// 否则账号永不冷却,调度器会让每个请求反复撞同一批 429 账号(旋转木马)。
|
||||
func TestHandle429_AnthropicNoResetTimeUsesFallbackCooldown(t *testing.T) {
|
||||
accountRepo := &rateLimit429AccountRepoStub{}
|
||||
settingRepo := newMockSettingRepo()
|
||||
data, _ := json.Marshal(RateLimit429CooldownSettings{Enabled: true, CooldownSeconds: 12})
|
||||
settingRepo.data[SettingKeyRateLimit429CooldownSettings] = string(data)
|
||||
|
||||
settingSvc := NewSettingService(settingRepo, &config.Config{})
|
||||
svc := NewRateLimitService(accountRepo, nil, &config.Config{}, nil, nil)
|
||||
svc.SetSettingService(settingSvc)
|
||||
|
||||
account := &Account{ID: 45, Platform: PlatformAnthropic, Type: AccountTypeOAuth}
|
||||
before := time.Now()
|
||||
svc.handle429(context.Background(), account, http.Header{}, []byte(`{"error":{"type":"rate_limit_error","message":"Extra usage required"}}`))
|
||||
after := time.Now()
|
||||
|
||||
require.Equal(t, 1, accountRepo.rateLimitCalls)
|
||||
require.Equal(t, int64(45), accountRepo.lastRateLimitID)
|
||||
require.True(t, !accountRepo.lastRateLimitReset.Before(before.Add(12*time.Second)) && !accountRepo.lastRateLimitReset.After(after.Add(12*time.Second)))
|
||||
}
|
||||
|
||||
// 管理端关闭兜底冷却时,Anthropic 无 reset 头的 429 保持旧行为:不标记账号。
|
||||
func TestHandle429_AnthropicNoResetTimeFallbackDisabledSkipsMark(t *testing.T) {
|
||||
accountRepo := &rateLimit429AccountRepoStub{}
|
||||
settingRepo := newMockSettingRepo()
|
||||
data, _ := json.Marshal(RateLimit429CooldownSettings{Enabled: false, CooldownSeconds: 12})
|
||||
settingRepo.data[SettingKeyRateLimit429CooldownSettings] = string(data)
|
||||
|
||||
settingSvc := NewSettingService(settingRepo, &config.Config{})
|
||||
svc := NewRateLimitService(accountRepo, nil, &config.Config{}, nil, nil)
|
||||
svc.SetSettingService(settingSvc)
|
||||
|
||||
account := &Account{ID: 46, Platform: PlatformAnthropic, Type: AccountTypeOAuth}
|
||||
svc.handle429(context.Background(), account, http.Header{}, []byte(`{"error":{"type":"rate_limit_error","message":"Extra usage required"}}`))
|
||||
|
||||
require.Zero(t, accountRepo.rateLimitCalls)
|
||||
}
|
||||
|
||||
func TestHandle429_FallbackUsesDefaultSecondsWhenSettingServiceMissing(t *testing.T) {
|
||||
accountRepo := &rateLimit429AccountRepoStub{}
|
||||
cfg := &config.Config{}
|
||||
|
||||
@@ -994,12 +994,15 @@ func (s *RateLimitService) handle429(ctx context.Context, account *Account, head
|
||||
}
|
||||
|
||||
// Anthropic 平台:没有限流重置时间的 429 可能是非真实限流(如 Extra usage required),
|
||||
// 不标记账号限流状态,直接透传错误给客户端
|
||||
// 不适合按 5h/7d 窗口长时间封禁;但完全不标记会导致账号永不冷却,
|
||||
// 调度器让每个请求反复撞同一批持续 429 的账号(failover 预算被白白烧掉,
|
||||
// 客户端稳定收到 429)。因此同样走可配置的秒级兜底回避,管理端可调大或关闭。
|
||||
if account.Platform == PlatformAnthropic {
|
||||
slog.Warn("rate_limit_429_no_reset_time_skipped",
|
||||
slog.Warn("rate_limit_429_no_reset_time",
|
||||
"account_id", account.ID,
|
||||
"platform", account.Platform,
|
||||
"reason", "no rate limit reset time in headers, likely not a real rate limit")
|
||||
s.apply429FallbackRateLimit(ctx, account, "anthropic_no_reset_time")
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user