From a071b27b417ab2f20630ffb438b9480c271d5cc4 Mon Sep 17 00:00:00 2001 From: li Date: Fri, 7 Aug 2026 19:44:43 +0800 Subject: [PATCH] =?UTF-8?q?fix(grok):=20405=20=E7=BA=B3=E5=85=A5=20failove?= =?UTF-8?q?r=20=E4=B8=8E=E4=B8=B4=E6=97=B6=E8=B8=A2=E5=8F=B7=EF=BC=8C?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20Grok=20=E4=BC=9A=E8=AF=9D=E7=B2=98?= =?UTF-8?q?=E6=80=A7=E9=94=81=E6=AD=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Grok 分组里若有账号的自定义 base_url 只实现 /v1/chat/completions,对 /v1/responses 固定返回 405,会话 sticky 到该账号后就永久锁死: 1. shouldFailoverUpstreamError 不含 405,请求不进换号循环 2. handleGrokAccountUpstreamError 的 switch 不含 405,账号不进临时不可调度 3. 账号仍可调度,sticky 不清理,客户端自动重连继续绑同一个坏号 改动两处: - shouldFailoverUpstreamError 增加 405,让请求可换号,sticky 随之更新 - handleGrokAccountUpstreamError 增加 405 → 30 分钟冷却,与 403 同级 Fixes #4668 --- .../internal/service/openai_gateway_grok.go | 2 ++ .../service/openai_gateway_grok_405_test.go | 29 +++++++++++++++++++ .../service/openai_gateway_grok_test.go | 7 +++++ .../service/openai_gateway_upstream_errors.go | 2 +- 4 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 backend/internal/service/openai_gateway_grok_405_test.go diff --git a/backend/internal/service/openai_gateway_grok.go b/backend/internal/service/openai_gateway_grok.go index 4d367c9aeb..87899c1350 100644 --- a/backend/internal/service/openai_gateway_grok.go +++ b/backend/internal/service/openai_gateway_grok.go @@ -1370,6 +1370,8 @@ func (s *OpenAIGatewayService) handleGrokAccountUpstreamError(ctx context.Contex s.tempUnscheduleGrok(ctx, account, 30*time.Minute, "grok payment required") case http.StatusForbidden: s.tempUnscheduleGrok(ctx, account, 30*time.Minute, "grok access or entitlement denied") + case http.StatusMethodNotAllowed: + s.tempUnscheduleGrok(ctx, account, 30*time.Minute, "grok endpoint not supported (405)") case http.StatusTooManyRequests: // updateGrokUsageSnapshot installs rate-limit state for non-pool accounts. default: diff --git a/backend/internal/service/openai_gateway_grok_405_test.go b/backend/internal/service/openai_gateway_grok_405_test.go new file mode 100644 index 0000000000..10ed8a599e --- /dev/null +++ b/backend/internal/service/openai_gateway_grok_405_test.go @@ -0,0 +1,29 @@ +package service + +import ( + "net/http" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestShouldFailoverUpstreamError_405IsFailoverEligible(t *testing.T) { + svc := &OpenAIGatewayService{} + + assert.True(t, svc.shouldFailoverUpstreamError(http.StatusMethodNotAllowed), + "405 should trigger failover so sticky sessions can escape to healthy accounts") +} + +func TestShouldFailoverUpstreamError_ExistingCodesStillWork(t *testing.T) { + svc := &OpenAIGatewayService{} + + failoverCodes := []int{401, 402, 403, 405, 429, 529, 500, 502, 503, 504} + for _, code := range failoverCodes { + assert.True(t, svc.shouldFailoverUpstreamError(code), "status %d should trigger failover", code) + } + + nonFailoverCodes := []int{200, 201, 400, 404, 408, 422} + for _, code := range nonFailoverCodes { + assert.False(t, svc.shouldFailoverUpstreamError(code), "status %d should NOT trigger failover", code) + } +} diff --git a/backend/internal/service/openai_gateway_grok_test.go b/backend/internal/service/openai_gateway_grok_test.go index 19942eae7d..30c25ba586 100644 --- a/backend/internal/service/openai_gateway_grok_test.go +++ b/backend/internal/service/openai_gateway_grok_test.go @@ -2583,6 +2583,13 @@ func TestHandleGrokAccountUpstreamErrorTempUnschedulesNonRateLimitStates(t *test wantMinCooldown: 2*time.Minute - time.Second, wantMaxCooldown: 2*time.Minute + time.Second, }, + { + name: "method not allowed", + status: http.StatusMethodNotAllowed, + wantReason: "grok endpoint not supported (405)", + wantMinCooldown: 30*time.Minute - time.Second, + wantMaxCooldown: 30*time.Minute + time.Second, + }, } for _, tt := range tests { diff --git a/backend/internal/service/openai_gateway_upstream_errors.go b/backend/internal/service/openai_gateway_upstream_errors.go index f56246e120..e0d3b8cf83 100644 --- a/backend/internal/service/openai_gateway_upstream_errors.go +++ b/backend/internal/service/openai_gateway_upstream_errors.go @@ -211,7 +211,7 @@ func isOpenAIContextWindowError(upstreamMsg string, upstreamBody []byte) bool { func (s *OpenAIGatewayService) shouldFailoverUpstreamError(statusCode int) bool { switch statusCode { - case 401, 402, 403, 429, 529: + case 401, 402, 403, 405, 429, 529: return true default: return statusCode >= 500