fix: omit Retry-After hint when any supporting account is not in persisted cooldown(audit follow-up for #9197)

This commit is contained in:
shaw committed 2026-12-02 11:42:27 +08:00
1 parent e7f32da0a8
commit 8aa5c2a4ba
3 files changed
+83

No files matched your search

@@ -109,6 +109,10 @@ func (s *GatewayService) DiagnoseModelAvailabilityForPlatform(
diag := ModelAvailabilityDiagnosis{}
var earliestRecovery time.Duration
// A supporting account with no persisted cooldown is waiting on a runtime
// filter (quota, RPM, window cost, ...) whose recovery time is unknown, so
// the pool-level hint must be omitted rather than inflated by a cooling peer.
hasUncooledCandidate := false
for i := range accounts {
if useMixed && accounts[i].Platform == PlatformAntigravity && !accounts[i].IsMixedSchedulingEnabled() {
continue
@@ -120,9 +124,14 @@ func (s *GatewayService) DiagnoseModelAvailabilityForPlatform(
if earliestRecovery == 0 || remaining < earliestRecovery {
earliestRecovery = remaining
}
} else {
hasUncooledCandidate = true
}
}
}
if hasUncooledCandidate {
earliestRecovery = 0
}
diag.RetryAfterSeconds = ceilRetryAfterSeconds(earliestRecovery, DownstreamRetryAfterMaxSeconds(s.cfg))
return diag
}
@@ -249,3 +249,68 @@ func TestDiagnoseModelAvailabilityForPlatform_WrongPlatformFiltersOut(t *testing
require.False(t, diag.HasAccountsInPool, "OpenAI route must not see Anthropic accounts in pool")
require.False(t, diag.HasModelSupport)
}
func retryAfterTestAccount(id int64, platform string, groupID int64, cooldown time.Duration) Account {
acc := Account{
ID: id,
Platform: platform,
Status: StatusActive,
Schedulable: true,
AccountGroups: []AccountGroup{{GroupID: groupID}},
Credentials: map[string]any{
"model_mapping": map[string]any{"claude-opus-4-8": "claude-opus-4-8"},
},
}
if cooldown > 0 {
until := time.Now().Add(cooldown)
acc.RateLimitResetAt = &until
}
return acc
}
func TestDiagnoseModelAvailability_RetryAfterHint(t *testing.T) {
groupID := int64(44)
tests := []struct {
name string
cooldowns []time.Duration
want func(t *testing.T, seconds int)
}{
{
name: "mixed pool with runtime-filtered account omits hint",
cooldowns: []time.Duration{0, 2 * time.Hour},
want: func(t *testing.T, s int) { require.Equal(t, 0, s) },
},
{
name: "all cooling picks earliest",
cooldowns: []time.Duration{90 * time.Second, 2 * time.Hour},
want: func(t *testing.T, s int) { require.InDelta(t, 90, s, 2) },
},
{
name: "all cooling is capped at the default maximum",
cooldowns: []time.Duration{2 * time.Hour, 3 * time.Hour},
want: func(t *testing.T, s int) { require.Equal(t, DownstreamRetryAfterMaxSeconds(nil), s) },
},
}
for _, tt := range tests {
t.Run("anthropic/"+tt.name, func(t *testing.T) {
repo := &mockAccountRepoForPlatform{accountsByID: map[int64]*Account{}}
for i, c := range tt.cooldowns {
repo.accounts = append(repo.accounts, retryAfterTestAccount(int64(i+1), PlatformAnthropic, groupID, c))
}
svc := &GatewayService{accountRepo: repo, cfg: testConfig(), schedulerSnapshot: &SchedulerSnapshotService{}}
diag := svc.DiagnoseModelAvailabilityForPlatform(context.Background(), &groupID, "claude-opus-4-8", PlatformAnthropic)
require.True(t, diag.HasModelSupport)
tt.want(t, diag.RetryAfterSeconds)
})
t.Run("openai/"+tt.name, func(t *testing.T) {
repo := &mockAccountRepoForPlatform{accountsByID: map[int64]*Account{}}
for i, c := range tt.cooldowns {
repo.accounts = append(repo.accounts, retryAfterTestAccount(int64(i+1), PlatformOpenAI, groupID, c))
}
svc := &OpenAIGatewayService{accountRepo: repo, cfg: testConfig(), schedulerSnapshot: &SchedulerSnapshotService{}}
diag := svc.DiagnoseModelAvailabilityForPlatform(context.Background(), &groupID, "claude-opus-4-8", PlatformOpenAI)
require.True(t, diag.HasModelSupport)
tt.want(t, diag.RetryAfterSeconds)
})
}
}
@@ -56,6 +56,10 @@ func (s *OpenAIGatewayService) DiagnoseModelAvailabilityForPlatform(
diag := ModelAvailabilityDiagnosis{}
var earliestRecovery time.Duration
// A supporting account with no persisted cooldown is waiting on a runtime
// filter (quota, RPM, window cost, ...) whose recovery time is unknown, so
// the pool-level hint must be omitted rather than inflated by a cooling peer.
hasUncooledCandidate := false
for i := range accounts {
diag.HasAccountsInPool = true
if isRetiredOpenAISubscriptionModel(&accounts[i], resolveOpenAIAccountUpstreamModelForRequest(&accounts[i], requestedModel, false)) {
@@ -71,9 +75,14 @@ func (s *OpenAIGatewayService) DiagnoseModelAvailabilityForPlatform(
if earliestRecovery == 0 || remaining < earliestRecovery {
earliestRecovery = remaining
}
} else {
hasUncooledCandidate = true
}
}
}
if hasUncooledCandidate {
earliestRecovery = 0
}
diag.RetryAfterSeconds = ceilRetryAfterSeconds(earliestRecovery, DownstreamRetryAfterMaxSeconds(s.cfg))
return diag
}