fix(auth): add retry logic for 429 status with Retry-After and improve testing

This commit is contained in:
Luis Pater
2026-04-09 07:07:19 +08:00
parent 941334da79
commit ad8e3964ff
2 changed files with 112 additions and 3 deletions
@@ -690,6 +690,57 @@ func TestManager_Execute_DisableCooling_DoesNotBlackoutAfter429RetryAfter(t *tes
}
}
func TestManager_Execute_DisableCooling_RetriesAfter429RetryAfter(t *testing.T) {
prev := quotaCooldownDisabled.Load()
quotaCooldownDisabled.Store(false)
t.Cleanup(func() { quotaCooldownDisabled.Store(prev) })
m := NewManager(nil, nil, nil)
m.SetRetryConfig(3, 100*time.Millisecond, 0)
executor := &authFallbackExecutor{
id: "claude",
executeErrors: map[string]error{
"auth-429-retryafter-exec": &retryAfterStatusError{
status: http.StatusTooManyRequests,
message: "quota exhausted",
retryAfter: 5 * time.Millisecond,
},
},
}
m.RegisterExecutor(executor)
auth := &Auth{
ID: "auth-429-retryafter-exec",
Provider: "claude",
Metadata: map[string]any{
"disable_cooling": true,
},
}
if _, errRegister := m.Register(context.Background(), auth); errRegister != nil {
t.Fatalf("register auth: %v", errRegister)
}
model := "test-model-429-retryafter-exec"
reg := registry.GetGlobalRegistry()
reg.RegisterClient(auth.ID, "claude", []*registry.ModelInfo{{ID: model}})
t.Cleanup(func() { reg.UnregisterClient(auth.ID) })
req := cliproxyexecutor.Request{Model: model}
_, errExecute := m.Execute(context.Background(), []string{"claude"}, req, cliproxyexecutor.Options{})
if errExecute == nil {
t.Fatal("expected execute error")
}
if statusCodeFromError(errExecute) != http.StatusTooManyRequests {
t.Fatalf("execute status = %d, want %d", statusCodeFromError(errExecute), http.StatusTooManyRequests)
}
calls := executor.ExecuteCalls()
if len(calls) != 4 {
t.Fatalf("execute calls = %d, want 4 (initial + 3 retries)", len(calls))
}
}
func TestManager_MarkResult_RequestScopedNotFoundDoesNotCooldownAuth(t *testing.T) {
m := NewManager(nil, nil, nil)