From 99331a528560423f18df564ba5c3b58369b4b67f Mon Sep 17 00:00:00 2001 From: erio Date: Thu, 5 Mar 2026 19:12:49 +0800 Subject: [PATCH] fix: throttle Anthropic usage queries and pass through upstream HTTP errors - Frontend: queue Anthropic OAuth/setup-token usage requests by proxy with random 1-1.5s interval to prevent upstream 429 - Backend: return ApplicationError with actual upstream status code instead of wrapping all errors as 500 - Handle component unmount to skip stale updates on page navigation --- backend/cmd/server/VERSION | 2 +- .../repository/claude_usage_service.go | 4 +- .../components/account/AccountUsageCell.vue | 32 +++++-- .../__tests__/AccountUsageCell.spec.ts | 4 + .../utils/__tests__/usageLoadQueue.spec.ts | 87 +++++++++++++++++++ frontend/src/utils/usageLoadQueue.ts | 72 +++++++++++++++ 6 files changed, 194 insertions(+), 7 deletions(-) create mode 100644 frontend/src/utils/__tests__/usageLoadQueue.spec.ts create mode 100644 frontend/src/utils/usageLoadQueue.ts diff --git a/backend/cmd/server/VERSION b/backend/cmd/server/VERSION index 2858d7c5..5a7989b0 100644 --- a/backend/cmd/server/VERSION +++ b/backend/cmd/server/VERSION @@ -1 +1 @@ -0.1.90.3 +0.1.90.4 diff --git a/backend/internal/repository/claude_usage_service.go b/backend/internal/repository/claude_usage_service.go index f6054828..e33f2e50 100644 --- a/backend/internal/repository/claude_usage_service.go +++ b/backend/internal/repository/claude_usage_service.go @@ -8,6 +8,7 @@ import ( "net/http" "time" + infraerrors "github.com/Wei-Shaw/sub2api/internal/pkg/errors" "github.com/Wei-Shaw/sub2api/internal/pkg/httpclient" "github.com/Wei-Shaw/sub2api/internal/service" ) @@ -95,7 +96,8 @@ func (s *claudeUsageService) FetchUsageWithOptions(ctx context.Context, opts *se if resp.StatusCode != http.StatusOK { body, _ := io.ReadAll(resp.Body) - return nil, fmt.Errorf("API returned status %d: %s", resp.StatusCode, string(body)) + msg := fmt.Sprintf("API returned status %d: %s", resp.StatusCode, string(body)) + return nil, infraerrors.New(resp.StatusCode, "UPSTREAM_ERROR", msg) } var usageResp service.ClaudeUsageResponse diff --git a/frontend/src/components/account/AccountUsageCell.vue b/frontend/src/components/account/AccountUsageCell.vue index 859bd7c9..24851afb 100644 --- a/frontend/src/components/account/AccountUsageCell.vue +++ b/frontend/src/components/account/AccountUsageCell.vue @@ -278,11 +278,12 @@