1
0
Fork 0

Handle context canceled in ForwardAuth middleware

This commit is contained in:
Ben 2025-06-04 07:38:04 -06:00 committed by GitHub
parent bf72b9768c
commit 2949995abc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 78 additions and 1 deletions

View file

@ -17,6 +17,7 @@ import (
"github.com/traefik/traefik/v3/pkg/middlewares"
"github.com/traefik/traefik/v3/pkg/middlewares/accesslog"
"github.com/traefik/traefik/v3/pkg/middlewares/observability"
"github.com/traefik/traefik/v3/pkg/proxy/httputil"
"github.com/traefik/traefik/v3/pkg/tracing"
"github.com/traefik/traefik/v3/pkg/types"
"github.com/vulcand/oxy/v2/forward"
@ -195,7 +196,12 @@ func (fa *forwardAuth) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
logger.Debug().Err(forwardErr).Msgf("Error calling %s", fa.address)
observability.SetStatusErrorf(req.Context(), "Error calling %s. Cause: %s", fa.address, forwardErr)
rw.WriteHeader(http.StatusInternalServerError)
statusCode := http.StatusInternalServerError
if errors.Is(forwardErr, context.Canceled) {
statusCode = httputil.StatusClientClosedRequest
}
rw.WriteHeader(statusCode)
return
}
defer forwardResponse.Body.Close()