From f636eec33bc8bcdbdb7d8597422236c2611a868b Mon Sep 17 00:00:00 2001 From: unkn0wn-root Date: Sat, 4 Jan 2025 20:48:34 +0100 Subject: [PATCH] fix(proxy): handle client disconnect from round tripper using proxy error --- internal/pool/errors.go | 14 +++++++------- internal/pool/proxy.go | 10 +++++++--- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/internal/pool/errors.go b/internal/pool/errors.go index efca01f..e2a760b 100644 --- a/internal/pool/errors.go +++ b/internal/pool/errors.go @@ -24,6 +24,13 @@ const ( RetryAfterSec = 5 ) +// ErrorResponse represents the structure of error responses sent to clients +type ErrorResponse struct { + Status string `json:"status"` + Message string `json:"message"` + RetryAfter int `json:"retry_after,omitempty"` +} + // ProxyErrorCode represents specific error conditions in the proxy type ProxyErrorCode int @@ -189,13 +196,6 @@ func NewProxyError(op string, err error) *ProxyError { return pe } -// ErrorResponse represents the structure of error responses sent to clients -type ErrorResponse struct { - Status string `json:"status"` - Message string `json:"message"` - RetryAfter int `json:"retry_after,omitempty"` -} - // WriteErrorResponse writes a structured error response to the client func WriteErrorResponse(w http.ResponseWriter, err error) { var pe *ProxyError diff --git a/internal/pool/proxy.go b/internal/pool/proxy.go index 73b7bdb..4f3ca8f 100644 --- a/internal/pool/proxy.go +++ b/internal/pool/proxy.go @@ -1,7 +1,6 @@ package pool import ( - "context" "crypto/tls" "errors" "fmt" @@ -242,10 +241,15 @@ func (p *URLRewriteProxy) errorHandler(w http.ResponseWriter, r *http.Request, e // so as for Go 1.23 this is still an issue and we have to live with it // We don't want to overflow logs with this error as this can happen quite often // so we just ignore it for now until Go team provide a better solution - if errors.Is(err, context.Canceled) { - return + var proxyErr *ProxyError + if errors.As(err, &proxyErr) { + if proxyErr.Code == ErrCodeClientDisconnect { + return + } } + // log every error even if error could be non-proxy + // WriteErrorResponse will handle all non-proxy errors but it should not happen (just in case) p.logger.Error("Proxy error", zap.Error(err), zap.String("method", r.Method),