mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-11-16 22:57:53 +08:00
perf,fix(logging,websocket): avoid 200 OK body buffering and synchronize ws disconnect error logging
This commit is contained in:
@@ -62,6 +62,7 @@ func RequestLoggingMiddleware(logger logging.RequestLogger) gin.HandlerFunc {
|
||||
|
||||
// Create response writer wrapper
|
||||
wrapper := NewResponseWriterWrapper(c.Writer, logger, requestInfo)
|
||||
wrapper.ginContext = c
|
||||
if !loggerEnabled {
|
||||
wrapper.logOnErrorOnly = true
|
||||
}
|
||||
|
||||
@@ -957,7 +957,7 @@ func TestDecodeCapturedZstdRefusesAnOversizedWindow(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestResponseWriterLogOnErrorOnlyCapsSingleLargeWrite(t *testing.T) {
|
||||
func TestResponseWriterLogOnErrorOnlyZeroBufferUntilActionableError(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
rec := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(rec)
|
||||
@@ -969,19 +969,29 @@ func TestResponseWriterLogOnErrorOnlyCapsSingleLargeWrite(t *testing.T) {
|
||||
Method: http.MethodPost,
|
||||
})
|
||||
wrapper.logOnErrorOnly = true
|
||||
wrapper.ginContext = c
|
||||
|
||||
// Normal 200 OK writes (even 4 MiB) must not allocate/buffer any bytes when request-log is disabled.
|
||||
largePayload := bytes.Repeat([]byte("a"), 4<<20) // 4 MiB
|
||||
if _, err := wrapper.Write(largePayload); err != nil {
|
||||
t.Fatalf("Write failed: %v", err)
|
||||
}
|
||||
if wrapper.body.Len() != maxDeferredErrorStreamBodyBytes {
|
||||
t.Fatalf("wrapper.body.Len() = %d, want %d", wrapper.body.Len(), maxDeferredErrorStreamBodyBytes)
|
||||
if wrapper.body.Len() != 0 {
|
||||
t.Fatalf("wrapper.body.Len() for 200 OK = %d, want 0", wrapper.body.Len())
|
||||
}
|
||||
|
||||
if _, err := wrapper.WriteString("extra"); err != nil {
|
||||
// Once an actionable mid-stream error is recorded, subsequent error frame writes are buffered.
|
||||
c.Set("API_RESPONSE_ERROR", []*interfaces.ErrorMessage{
|
||||
{
|
||||
StatusCode: http.StatusBadRequest,
|
||||
Error: errors.New("claude refusal (cyber): blocked"),
|
||||
},
|
||||
})
|
||||
const errFrame = "event: error\ndata: {\"type\":\"error\"}\n\n"
|
||||
if _, err := wrapper.WriteString(errFrame); err != nil {
|
||||
t.Fatalf("WriteString failed: %v", err)
|
||||
}
|
||||
if wrapper.body.Len() != maxDeferredErrorStreamBodyBytes {
|
||||
t.Fatalf("wrapper.body.Len() after WriteString = %d, want %d", wrapper.body.Len(), maxDeferredErrorStreamBodyBytes)
|
||||
if got := wrapper.body.String(); got != errFrame {
|
||||
t.Fatalf("wrapper.body.String() = %q, want %q", got, errFrame)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,6 +47,7 @@ type ResponseWriterWrapper struct {
|
||||
streamDone chan struct{} // streamDone signals when the streaming goroutine completes.
|
||||
logger logging.RequestLogger // logger is the instance of the request logger service.
|
||||
requestInfo *RequestInfo // requestInfo holds the details of the original request.
|
||||
ginContext *gin.Context // ginContext holds the Gin request context for mid-stream error inspection.
|
||||
statusCode int // statusCode stores the HTTP status code of the response.
|
||||
headers map[string][]string // headers stores the response headers.
|
||||
logOnErrorOnly bool // logOnErrorOnly enables logging only when an error response is detected.
|
||||
@@ -119,7 +120,9 @@ func (w *ResponseWriterWrapper) Write(data []byte) (int, error) {
|
||||
return n, err
|
||||
}
|
||||
|
||||
w.bufferResponseBodyBytes(data)
|
||||
if w.shouldBufferResponseBody() {
|
||||
w.body.Write(data)
|
||||
}
|
||||
|
||||
return n, err
|
||||
}
|
||||
@@ -161,8 +164,6 @@ func (w *ResponseWriterWrapper) closeConnectionIfRequestBodyUnconsumed(statusCod
|
||||
}
|
||||
}
|
||||
|
||||
const maxDeferredErrorStreamBodyBytes = 256 << 10 // 256 KiB
|
||||
|
||||
func (w *ResponseWriterWrapper) shouldBufferResponseBody() bool {
|
||||
if w.logger != nil && w.logger.IsEnabled() {
|
||||
return true
|
||||
@@ -171,57 +172,12 @@ func (w *ResponseWriterWrapper) shouldBufferResponseBody() bool {
|
||||
return false
|
||||
}
|
||||
status := w.effectiveStatusCode()
|
||||
if status == clienterror.StatusClientClosedRequest {
|
||||
return false
|
||||
if status < http.StatusBadRequest {
|
||||
if apiErrStatus := actionableAPIResponseErrorStatus(extractAPIResponseErrors(w.ginContext)); apiErrStatus >= http.StatusBadRequest {
|
||||
status = apiErrStatus
|
||||
}
|
||||
}
|
||||
if status >= http.StatusBadRequest {
|
||||
return true
|
||||
}
|
||||
return w.body != nil && w.body.Len() < maxDeferredErrorStreamBodyBytes
|
||||
}
|
||||
|
||||
func (w *ResponseWriterWrapper) bufferResponseBodyBytes(data []byte) {
|
||||
if len(data) == 0 || w.body == nil || !w.shouldBufferResponseBody() {
|
||||
return
|
||||
}
|
||||
if w.logger != nil && w.logger.IsEnabled() {
|
||||
w.body.Write(data)
|
||||
return
|
||||
}
|
||||
if w.effectiveStatusCode() >= http.StatusBadRequest {
|
||||
w.body.Write(data)
|
||||
return
|
||||
}
|
||||
remaining := maxDeferredErrorStreamBodyBytes - w.body.Len()
|
||||
if remaining <= 0 {
|
||||
return
|
||||
}
|
||||
if len(data) > remaining {
|
||||
data = data[:remaining]
|
||||
}
|
||||
w.body.Write(data)
|
||||
}
|
||||
|
||||
func (w *ResponseWriterWrapper) bufferResponseBodyString(data string) {
|
||||
if len(data) == 0 || w.body == nil || !w.shouldBufferResponseBody() {
|
||||
return
|
||||
}
|
||||
if w.logger != nil && w.logger.IsEnabled() {
|
||||
w.body.WriteString(data)
|
||||
return
|
||||
}
|
||||
if w.effectiveStatusCode() >= http.StatusBadRequest {
|
||||
w.body.WriteString(data)
|
||||
return
|
||||
}
|
||||
remaining := maxDeferredErrorStreamBodyBytes - w.body.Len()
|
||||
if remaining <= 0 {
|
||||
return
|
||||
}
|
||||
if len(data) > remaining {
|
||||
data = data[:remaining]
|
||||
}
|
||||
w.body.WriteString(data)
|
||||
return status >= http.StatusBadRequest && status != clienterror.StatusClientClosedRequest
|
||||
}
|
||||
|
||||
// WriteString wraps the underlying ResponseWriter's WriteString method to capture response data.
|
||||
@@ -247,7 +203,9 @@ func (w *ResponseWriterWrapper) WriteString(data string) (int, error) {
|
||||
return n, err
|
||||
}
|
||||
|
||||
w.bufferResponseBodyString(data)
|
||||
if w.shouldBufferResponseBody() {
|
||||
w.body.WriteString(data)
|
||||
}
|
||||
return n, err
|
||||
}
|
||||
|
||||
@@ -894,6 +852,18 @@ func hasActionableAPIResponseErrors(apiErrors []*interfaces.ErrorMessage) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func extractAPIResponseErrors(c *gin.Context) []*interfaces.ErrorMessage {
|
||||
if c == nil {
|
||||
return nil
|
||||
}
|
||||
if apiResponseError, exists := c.Get("API_RESPONSE_ERROR"); exists {
|
||||
if apiErrors, ok := apiResponseError.([]*interfaces.ErrorMessage); ok {
|
||||
return apiErrors
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func actionableAPIResponseErrorStatus(apiErrors []*interfaces.ErrorMessage) int {
|
||||
for i := len(apiErrors) - 1; i >= 0; i-- {
|
||||
errMsg := apiErrors[i]
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
package clienterror
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
@@ -160,7 +161,7 @@ const GeminiFilterBlockPolicyURL = "https://policies.google.com/terms/generative
|
||||
// Upstream injects this frame inside an HTTP 200 SSE stream with only candidates[0].content.parts[0].text
|
||||
// and without content.role, responseId, modelVersion, or usageMetadata.
|
||||
func GeminiFilterBlockMessage(payload []byte) (string, bool) {
|
||||
if len(payload) == 0 || !ojson.ValidBytes(payload) {
|
||||
if len(payload) == 0 || !bytes.Contains(payload, []byte(GeminiFilterBlockMessagePrefix)) || !ojson.ValidBytes(payload) {
|
||||
return "", false
|
||||
}
|
||||
for _, path := range []string{"responseId", "response.responseId", "modelVersion", "response.modelVersion", "usageMetadata", "response.usageMetadata"} {
|
||||
@@ -220,7 +221,7 @@ func isGeminiFilterBlockText(text string) bool {
|
||||
// message object or a streaming message_delta event) and returns the refusal explanation
|
||||
// when stop_details indicates a policy/cyber refusal block.
|
||||
func ClaudePolicyRefusalMessage(payload []byte) (string, bool) {
|
||||
if len(payload) == 0 || !ojson.ValidBytes(payload) {
|
||||
if len(payload) == 0 || !bytes.Contains(payload, []byte("stop_details")) || !ojson.ValidBytes(payload) {
|
||||
return "", false
|
||||
}
|
||||
for _, prefix := range []string{"delta", ""} {
|
||||
|
||||
@@ -334,6 +334,10 @@ func (h *ClaudeCodeAPIHandler) handleStreamingResponse(c *gin.Context, rawJSON [
|
||||
var streamState claudeSSEStreamState
|
||||
streamState.trailingNewlines = 2
|
||||
streamState.ObserveChunk(chunk)
|
||||
if streamState.policyRefusalErr != nil && !streamState.loggedTerminalError {
|
||||
h.LoggingAPIResponseError(c, streamState.policyRefusalErr)
|
||||
streamState.loggedTerminalError = true
|
||||
}
|
||||
if len(chunk) > 0 {
|
||||
_, _ = c.Writer.Write(chunk)
|
||||
flusher.Flush()
|
||||
@@ -349,13 +353,15 @@ func (h *ClaudeCodeAPIHandler) handleStreamingResponse(c *gin.Context, rawJSON [
|
||||
}
|
||||
|
||||
type claudeSSEStreamState struct {
|
||||
sawErrorEvent bool
|
||||
sawMessageStop bool
|
||||
sawBareMessageJSON bool
|
||||
trailingNewlines int
|
||||
currentEvent string
|
||||
eventData [][]byte
|
||||
lineBuf bytes.Buffer
|
||||
sawErrorEvent bool
|
||||
sawMessageStop bool
|
||||
sawBareMessageJSON bool
|
||||
loggedTerminalError bool
|
||||
policyRefusalErr *interfaces.ErrorMessage
|
||||
trailingNewlines int
|
||||
currentEvent string
|
||||
eventData [][]byte
|
||||
lineBuf bytes.Buffer
|
||||
}
|
||||
|
||||
func (s *claudeSSEStreamState) ObserveChunk(chunk []byte) {
|
||||
@@ -429,6 +435,14 @@ func (s *claudeSSEStreamState) observeLine(line []byte) {
|
||||
}
|
||||
|
||||
func (s *claudeSSEStreamState) observeJSONPayload(payload []byte, hasDataPrefix bool) {
|
||||
if s.policyRefusalErr == nil {
|
||||
if refusalMsg, blocked := clienterror.ClaudePolicyRefusalMessage(payload); blocked {
|
||||
s.policyRefusalErr = &interfaces.ErrorMessage{
|
||||
StatusCode: http.StatusBadRequest,
|
||||
Error: errors.New(refusalMsg),
|
||||
}
|
||||
}
|
||||
}
|
||||
root := util.ParseGJSONBytesNoCopy(payload)
|
||||
switch root.Get("type").String() {
|
||||
case "message_stop":
|
||||
@@ -482,6 +496,10 @@ func (h *ClaudeCodeAPIHandler) forwardClaudeStreamWithState(c *gin.Context, flus
|
||||
return
|
||||
}
|
||||
state.ObserveChunk(chunk)
|
||||
if state.policyRefusalErr != nil && !state.loggedTerminalError {
|
||||
h.LoggingAPIResponseError(c, state.policyRefusalErr)
|
||||
state.loggedTerminalError = true
|
||||
}
|
||||
_, _ = c.Writer.Write(chunk)
|
||||
},
|
||||
WriteTerminalError: func(errMsg *interfaces.ErrorMessage) {
|
||||
@@ -489,7 +507,10 @@ func (h *ClaudeCodeAPIHandler) forwardClaudeStreamWithState(c *gin.Context, flus
|
||||
if errMsg == nil {
|
||||
return
|
||||
}
|
||||
h.LoggingAPIResponseError(c, errMsg)
|
||||
if !state.loggedTerminalError {
|
||||
h.LoggingAPIResponseError(c, errMsg)
|
||||
state.loggedTerminalError = true
|
||||
}
|
||||
if state.sawErrorEvent || (state.sawMessageStop && clienterror.IsClaudePolicyRefusalError(errMsg.Error)) {
|
||||
state.ensureSSEEventBoundary(c.Writer)
|
||||
return
|
||||
|
||||
@@ -201,30 +201,30 @@ func (w *responsesWebsocketWriter) closeWithPayload(payload []byte) (bool, error
|
||||
return true, errClose
|
||||
}
|
||||
|
||||
func (w *responsesWebsocketWriter) closeForUpstreamDisconnect(err error, wsTimelineLog ...websocketTimelineAppender) {
|
||||
func (w *responsesWebsocketWriter) closeForUpstreamDisconnect(err error, wsTimelineLog ...websocketTimelineAppender) bool {
|
||||
if w == nil || w.conn == nil {
|
||||
return
|
||||
return false
|
||||
}
|
||||
var timelineLog websocketTimelineAppender
|
||||
if len(wsTimelineLog) > 0 {
|
||||
timelineLog = wsTimelineLog[0]
|
||||
}
|
||||
if matched, _ := w.closeForUpstreamError(err); matched {
|
||||
return
|
||||
return true
|
||||
}
|
||||
|
||||
errMsg := handlers.ExecutionErrorMessage(err)
|
||||
if !shouldExposeResponsesUpstreamError(errMsg) {
|
||||
if timelineLog != nil && errMsg != nil {
|
||||
closed, _ := w.closeWithoutError()
|
||||
if closed && timelineLog != nil && errMsg != nil {
|
||||
appendWebsocketTimelineDisconnect(timelineLog, errMsg.Error, time.Now())
|
||||
}
|
||||
_, _ = w.closeWithoutError()
|
||||
return
|
||||
return closed
|
||||
}
|
||||
payload, errBuild := buildResponsesWebsocketErrorPayload(errMsg)
|
||||
if errBuild != nil {
|
||||
_, _ = w.closeWithoutError()
|
||||
return
|
||||
closed, _ := w.closeWithoutError()
|
||||
return closed
|
||||
}
|
||||
wrote, errClose := w.closeWithPayload(payload)
|
||||
if wrote {
|
||||
@@ -240,6 +240,7 @@ func (w *responsesWebsocketWriter) closeForUpstreamDisconnect(err error, wsTimel
|
||||
if errClose != nil && !errors.Is(errClose, websocket.ErrCloseSent) {
|
||||
log.Debugf("responses websocket: upstream disconnect close failed: %v", errClose)
|
||||
}
|
||||
return wrote
|
||||
}
|
||||
|
||||
// isWebsocketConnectionClosedError reports whether the error only means the
|
||||
@@ -400,7 +401,8 @@ func (h *OpenAIResponsesAPIHandler) ResponsesWebsocket(c *gin.Context) {
|
||||
log.Infof("responses websocket: client connected id=%s remote=%s", passthroughSessionID, clientIP)
|
||||
|
||||
wsDone := make(chan struct{})
|
||||
defer close(wsDone)
|
||||
var disconnectWg sync.WaitGroup
|
||||
var recordedDisconnectErr atomic.Pointer[interfaces.ErrorMessage]
|
||||
var codexDuplexStream atomic.Bool
|
||||
|
||||
if h != nil && h.AuthManager != nil {
|
||||
@@ -415,7 +417,9 @@ func (h *OpenAIResponsesAPIHandler) ResponsesWebsocket(c *gin.Context) {
|
||||
if subscriber, ok := exec.(upstreamDisconnectSubscriber); ok && subscriber != nil {
|
||||
disconnectCh := subscriber.UpstreamDisconnectChan(passthroughSessionID)
|
||||
if disconnectCh != nil {
|
||||
disconnectWg.Add(1)
|
||||
go func() {
|
||||
defer disconnectWg.Done()
|
||||
select {
|
||||
case <-wsDone:
|
||||
return
|
||||
@@ -426,10 +430,9 @@ func (h *OpenAIResponsesAPIHandler) ResponsesWebsocket(c *gin.Context) {
|
||||
// OAuth-only steering still leaves API keys in normal mode.
|
||||
return
|
||||
}
|
||||
if disconnectErr != nil {
|
||||
h.LoggingAPIResponseError(context.WithValue(context.Background(), "gin", c), handlers.ExecutionErrorMessage(disconnectErr))
|
||||
if writer.closeForUpstreamDisconnect(disconnectErr, wsTimelineLog) && disconnectErr != nil {
|
||||
recordedDisconnectErr.CompareAndSwap(nil, handlers.ExecutionErrorMessage(disconnectErr))
|
||||
}
|
||||
writer.closeForUpstreamDisconnect(disconnectErr, wsTimelineLog)
|
||||
}
|
||||
}()
|
||||
}
|
||||
@@ -450,10 +453,16 @@ func (h *OpenAIResponsesAPIHandler) ResponsesWebsocket(c *gin.Context) {
|
||||
h.AuthManager.CloseExecutionSession(passthroughSessionID)
|
||||
log.Infof("responses websocket: upstream execution session closed id=%s", passthroughSessionID)
|
||||
}
|
||||
close(wsDone)
|
||||
cancelSocket(nil)
|
||||
_, _ = writer.closeWithoutError()
|
||||
if errClose := conn.Close(); errClose != nil && !isWebsocketConnectionClosedError(errClose) {
|
||||
log.Warnf("responses websocket: close connection error: %v", errClose)
|
||||
}
|
||||
disconnectWg.Wait()
|
||||
if errMsg := recordedDisconnectErr.Load(); errMsg != nil {
|
||||
h.LoggingAPIResponseError(c, errMsg)
|
||||
}
|
||||
// Finish control-frame diagnostics before snapshotting the timeline.
|
||||
<-inputDone
|
||||
wsTimelineLog.SetContext(c)
|
||||
|
||||
Reference in New Issue
Block a user