From b97bd1146b35046f62726ff2174c6543cf2f5d3b Mon Sep 17 00:00:00 2001 From: tbphp Date: Tue, 10 Jun 2025 14:50:51 +0800 Subject: [PATCH] feat: ignorableStreamErrors --- internal/proxy/server.go | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/internal/proxy/server.go b/internal/proxy/server.go index 7141afe..b3d99bb 100644 --- a/internal/proxy/server.go +++ b/internal/proxy/server.go @@ -20,6 +20,24 @@ import ( "github.com/sirupsen/logrus" ) +// A list of errors that are considered normal during streaming when a client disconnects. +var ignorableStreamErrors = []string{ + "context canceled", + "connection reset by peer", +} + +// isIgnorableStreamError checks if the error is a common, non-critical error that can occur +// when a client disconnects during a streaming response. +func isIgnorableStreamError(err error) bool { + errStr := err.Error() + for _, ignorableError := range ignorableStreamErrors { + if strings.Contains(errStr, ignorableError) { + return true + } + } + return false +} + // ProxyServer represents the proxy server type ProxyServer struct { keyManager types.KeyManager @@ -391,7 +409,11 @@ func (ps *ProxyServer) handleStreamingResponse(c *gin.Context, resp *http.Respon } if err != nil { if err != io.EOF { - logrus.Errorf("Error reading streaming response: %v", err) + if isIgnorableStreamError(err) { + logrus.Debugf("Stream closed by client or network: %v", err) + } else { + logrus.Errorf("Error reading streaming response: %v", err) + } } break }