mirror of
https://github.com/chenyme/grok2api.git
synced 2026-12-06 16:01:07 +00:00
fix: limit stream failure attempts to 2xx responses
This commit is contained in:
1 parent
c7805f823c
commit
cf13fe1a2d
4 files changed
+25
-5
No files matched your search
@@ -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
|
||||
|
||||
@@ -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}
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in new issue
Block a user