From cf13fe1a2db4ba6280e7e96d80b54e01aca534ea Mon Sep 17 00:00:00 2001 From: Chenyme <118253778+chenyme@users.noreply.github.com> Date: Mon, 31 Aug 2026 11:28:07 +0800 Subject: [PATCH] fix: limit stream failure attempts to 2xx responses --- backend/internal/application/gateway/attempt.go | 7 +++---- backend/internal/application/gateway/attempt_test.go | 10 ++++++++++ backend/internal/application/gateway/service.go | 2 +- backend/internal/application/gateway/service_test.go | 11 +++++++++++ 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/backend/internal/application/gateway/attempt.go b/backend/internal/application/gateway/attempt.go index 76663394..5db3627a 100644 --- a/backend/internal/application/gateway/attempt.go +++ b/backend/internal/application/gateway/attempt.go @@ -181,11 +181,10 @@ func (r *failureAttemptRecorder) hasStreamFailureFor(accountID uint64) bool { return false } -// ensureStreamFailureAttempt records the handed-off 2xx stream when it later -// fails. Prior 4xx/transport attempts must not suppress this row — otherwise -// diagnostics only show the first 429 while the list account is the retry. +// ensureStreamFailureAttempt 补记已完成 2xx 响应头握手、但随后失败的流式尝试。 +// 既有 4xx 或传输失败记录不能屏蔽该尝试;同一账号已有 response_stream 时不重复追加。 func (r *failureAttemptRecorder) ensureStreamFailureAttempt(credential accountdomain.Credential, startedAt time.Time, response *provider.Response, errorCode string) { - if response == nil || r.hasStreamFailureFor(credential.ID) { + if response == nil || response.StatusCode < http.StatusOK || response.StatusCode >= http.StatusMultipleChoices || r.hasStreamFailureFor(credential.ID) { return } statusCode := response.StatusCode diff --git a/backend/internal/application/gateway/attempt_test.go b/backend/internal/application/gateway/attempt_test.go index 77e9d257..e03928a4 100644 --- a/backend/internal/application/gateway/attempt_test.go +++ b/backend/internal/application/gateway/attempt_test.go @@ -124,6 +124,16 @@ func TestEnsureStreamFailureAttemptKeepsPrior429AndSkipsDuplicate(t *testing.T) } } +func TestEnsureStreamFailureAttemptSkipsNon2xx(t *testing.T) { + recorder := newFailureAttemptRecorder(http.MethodPost, "/responses") + credential := account.Credential{ID: 12, Name: "retry-b"} + forbidden := &provider.Response{StatusCode: http.StatusForbidden, Status: "403 Forbidden"} + recorder.ensureStreamFailureAttempt(credential, time.Now(), forbidden, "upstream_forbidden") + if got := recorder.snapshot(); len(got) != 0 { + t.Fatalf("non-2xx stream attempt = %#v", got) + } +} + func TestFailureAttemptRecorderClassifiesTransportErrorChain(t *testing.T) { dnsErr := &net.DNSError{Err: "no such host", Name: "api.example.test", IsNotFound: true} requestErr := &url.Error{Op: "Post", URL: "https://user:password@api.example.test/v1/responses?token=secret", Err: dnsErr} diff --git a/backend/internal/application/gateway/service.go b/backend/internal/application/gateway/service.go index ea693ec9..539053e4 100644 --- a/backend/internal/application/gateway/service.go +++ b/backend/internal/application/gateway/service.go @@ -1118,7 +1118,7 @@ func (s *Service) createResponseAt(ctx context.Context, input Input, path string } record.DurationMS = time.Since(startedAt).Milliseconds() record.ErrorCode = errorCode - if !successful { + if !successful && response.StatusCode >= http.StatusOK && response.StatusCode < http.StatusMultipleChoices { failureAttempts.ensureStreamFailureAttempt(credential, upstreamStartedAt, response, errorCode) } attempts := failureAttempts.snapshot() diff --git a/backend/internal/application/gateway/service_test.go b/backend/internal/application/gateway/service_test.go index 89ee48b2..54bb06a9 100644 --- a/backend/internal/application/gateway/service_test.go +++ b/backend/internal/application/gateway/service_test.go @@ -3064,6 +3064,17 @@ func TestGatewaySafetyRejectionDoesNotTouchAccountState(t *testing.T) { } } } + logs, total, err := auditRepo.List(ctx, 0, 10) + if err != nil || total != 1 || len(logs) != 1 { + t.Fatalf("audit list = %#v, total=%d, err=%v", logs, total, err) + } + detail, err := auditRepo.Get(ctx, logs[0].ID) + if err != nil { + t.Fatal(err) + } + if detail.AttemptCount != 1 || len(detail.Attempts) != 1 || detail.Attempts[0].Stage != "upstream_response" || detail.Attempts[0].UpstreamStatusCode == nil || *detail.Attempts[0].UpstreamStatusCode != http.StatusForbidden { + t.Fatalf("terminal 403 attempts = %#v", detail.Attempts) + } } func TestGatewayConsoleDPoPRequirementStopsAfterOneAccount(t *testing.T) {