Merge pull request #1036 from chenyme/retry

fix: cap account cooldown from upstream Retry-After
This commit is contained in:
Chenyme authored and GitHub committed 2026-09-02 18:10:03 +08:00
commit 876d394704
2 files changed
+140

No files matched your search

@@ -1271,6 +1271,7 @@ func (s *Selector) markMissingThinking(ctx context.Context, credential account.C
}
func (s *Selector) MarkFailure(ctx context.Context, credential account.Credential, status int, retryAfter time.Duration) {
retryAfter = s.boundUpstreamRetryAfter(retryAfter)
_ = s.markFailure(ctx, credential, credential.FailureCount, credential.FailureCount+1, status, retryAfter, status == 0)
}
@@ -1285,9 +1286,21 @@ func (s *Selector) MarkFailureAfterSuccess(ctx context.Context, credential accou
// applying the bounded, non-accumulating health penalty used for transient
// network and provider-wide failures.
func (s *Selector) markSoftFailure(ctx context.Context, credential account.Credential, status int, retryAfter time.Duration) error {
retryAfter = s.boundUpstreamRetryAfter(retryAfter)
return s.markFailure(ctx, credential, credential.FailureCount, credential.FailureCount+1, status, retryAfter, true)
}
func (s *Selector) boundUpstreamRetryAfter(retryAfter time.Duration) time.Duration {
if retryAfter <= 0 {
return retryAfter
}
_, _, cooldownMax, _ := s.routingConfig()
if cooldownMax > 0 && retryAfter > cooldownMax {
return cooldownMax
}
return retryAfter
}
func (s *Selector) markFailure(ctx context.Context, credential account.Credential, baselineFailureCount, nextFailureCount, status int, retryAfter time.Duration, soft bool) error {
_, cooldownBase, cooldownMax, _ := s.routingConfig()
// Soft failures only isolate this account briefly and never accumulate the
@@ -1800,6 +1800,133 @@ func TestMarkFailureSoftNetworkCooldown(t *testing.T) {
}
}
func TestBoundUpstreamRetryAfter(t *testing.T) {
selector := &Selector{cooldownMax: time.Minute}
tests := []struct {
name string
input time.Duration
want time.Duration
}{
{name: "empty", input: 0, want: 0},
{name: "negative", input: -time.Second, want: -time.Second},
{name: "within maximum", input: 30 * time.Second, want: 30 * time.Second},
{name: "above maximum", input: 2 * time.Minute, want: time.Minute},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if got := selector.boundUpstreamRetryAfter(test.input); got != test.want {
t.Fatalf("boundUpstreamRetryAfter(%s) = %s, want %s", test.input, got, test.want)
}
})
}
}
func TestMarkFailureBoundsUpstreamRetryAfter(t *testing.T) {
ctx := context.Background()
database, err := relational.OpenSQLite(ctx, filepath.Join(t.TempDir(), "retry-after-bound.db"))
if err != nil {
t.Fatal(err)
}
defer database.Close()
if err := database.InitializeSchema(ctx); err != nil {
t.Fatal(err)
}
accounts := relational.NewAccountRepository(database)
credential, _, err := accounts.UpsertByIdentity(ctx, account.Credential{
Provider: account.ProviderBuild, Name: "retry-after-bound", SourceKey: "retry-after-bound", EncryptedAccessToken: "encrypted", Enabled: true,
AuthStatus: account.AuthStatusActive, Priority: 10, MaxConcurrent: 1,
})
if err != nil {
t.Fatal(err)
}
selector := NewSelector(accounts, memory.NewConcurrencyLimiter(), memory.NewStickyStore(), nil, time.Hour, time.Second, time.Minute, 500*time.Millisecond)
before := time.Now().UTC()
selector.MarkFailure(ctx, credential, http.StatusTooManyRequests, 24*time.Hour)
updated, err := accounts.Get(ctx, credential.ID)
if err != nil {
t.Fatal(err)
}
if updated.CooldownUntil == nil {
t.Fatal("expected cooldown")
}
cooldown := updated.CooldownUntil.Sub(before)
if cooldown < 50*time.Second || cooldown > 70*time.Second {
t.Fatalf("upstream Retry-After cooldown = %s, want at most cooldownMax (~1m)", cooldown)
}
}
func TestMarkSoftFailureBoundsUpstreamRetryAfter(t *testing.T) {
ctx := context.Background()
database, err := relational.OpenSQLite(ctx, filepath.Join(t.TempDir(), "soft-retry-after-bound.db"))
if err != nil {
t.Fatal(err)
}
defer database.Close()
if err := database.InitializeSchema(ctx); err != nil {
t.Fatal(err)
}
accounts := relational.NewAccountRepository(database)
credential, _, err := accounts.UpsertByIdentity(ctx, account.Credential{
Provider: account.ProviderBuild, Name: "soft-retry-after-bound", SourceKey: "soft-retry-after-bound", EncryptedAccessToken: "encrypted", Enabled: true,
AuthStatus: account.AuthStatusActive, Priority: 10, MaxConcurrent: 1,
})
if err != nil {
t.Fatal(err)
}
selector := NewSelector(accounts, memory.NewConcurrencyLimiter(), memory.NewStickyStore(), nil, time.Hour, time.Second, time.Minute, 500*time.Millisecond)
before := time.Now().UTC()
if err := selector.markSoftFailure(ctx, credential, http.StatusServiceUnavailable, 24*time.Hour); err != nil {
t.Fatal(err)
}
updated, err := accounts.Get(ctx, credential.ID)
if err != nil {
t.Fatal(err)
}
if updated.CooldownUntil == nil {
t.Fatal("expected cooldown")
}
cooldown := updated.CooldownUntil.Sub(before)
if cooldown < 50*time.Second || cooldown > 70*time.Second {
t.Fatalf("soft upstream Retry-After cooldown = %s, want at most cooldownMax (~1m)", cooldown)
}
}
func TestMarkFailureAfterSuccessPreservesExplicitCooldown(t *testing.T) {
ctx := context.Background()
database, err := relational.OpenSQLite(ctx, filepath.Join(t.TempDir(), "after-success-cooldown.db"))
if err != nil {
t.Fatal(err)
}
defer database.Close()
if err := database.InitializeSchema(ctx); err != nil {
t.Fatal(err)
}
accounts := relational.NewAccountRepository(database)
credential, _, err := accounts.UpsertByIdentity(ctx, account.Credential{
Provider: account.ProviderBuild, Name: "after-success-cooldown", SourceKey: "after-success-cooldown", EncryptedAccessToken: "encrypted", Enabled: true,
AuthStatus: account.AuthStatusActive, Priority: 10, MaxConcurrent: 1,
})
if err != nil {
t.Fatal(err)
}
selector := NewSelector(accounts, memory.NewConcurrencyLimiter(), memory.NewStickyStore(), nil, time.Hour, time.Second, time.Minute, 500*time.Millisecond)
before := time.Now().UTC()
if err := selector.MarkFailureAfterSuccess(ctx, credential, http.StatusGatewayTimeout, 2*time.Hour); err != nil {
t.Fatal(err)
}
updated, err := accounts.Get(ctx, credential.ID)
if err != nil {
t.Fatal(err)
}
if updated.CooldownUntil == nil {
t.Fatal("expected cooldown")
}
cooldown := updated.CooldownUntil.Sub(before)
if cooldown < 119*time.Minute || cooldown > 121*time.Minute {
t.Fatalf("explicit after-success cooldown = %s, want ~2h", cooldown)
}
}
func TestConsoleRateLimitReconciliationUsesNonAccumulatingCooldown(t *testing.T) {
tests := []struct {
name string