Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: in errorHandler make sure to return the correct error #94

Merged
merged 1 commit into from
Sep 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 40 additions & 25 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,34 +195,49 @@ type BackendStats struct {
DowntimeStart time.Time
}

const errMessage = `<?xml version="1.0" encoding="UTF-8"?><Error><Code>BackendDown</Code><Message>The remote server returned an error (%v)</Message><Resource>%s</Resource></Error>`

func writeErrorResponse(w http.ResponseWriter, r *http.Request, err error) {
// Set retry-after header to indicate user-agents to retry request after 120secs.
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After
w.Header().Set("Retry-After", "120")
w.WriteHeader(http.StatusBadGateway)
harshavardhana marked this conversation as resolved.
Show resolved Hide resolved
w.Header().Set("Content-Type", "application/xml")
fmt.Fprintf(w, errMessage, err, r.URL.Path)
}

// ErrorHandler called by httputil.ReverseProxy for errors.
// Avoid canceled context error since it means the client disconnected.
func (b *Backend) ErrorHandler(_ http.ResponseWriter, _ *http.Request, err error) {
if err != nil {
offline := true
for _, nerr := range []error{
context.Canceled,
io.EOF,
io.ErrClosedPipe,
io.ErrUnexpectedEOF,
errors.New("http: server closed idle connection"),
} {
if errors.Is(err, nerr) {
offline = false
break
}
if err.Error() == nerr.Error() {
offline = false
break
}
}
if offline {
if globalLoggingEnabled {
logMsg(logMessage{Endpoint: b.endpoint, Status: "down", Error: err})
}
b.setOffline()
func (b *Backend) ErrorHandler(w http.ResponseWriter, r *http.Request, err error) {
if err == nil {
panic("reverse proxy cannot call error handler without err being set")
}

offline := true
for _, nerr := range []error{
context.Canceled,
io.EOF,
io.ErrClosedPipe,
io.ErrUnexpectedEOF,
errors.New("http: server closed idle connection"),
} {
if errors.Is(err, nerr) {
offline = false
break
}
if err.Error() == nerr.Error() {
offline = false
break
}
}
if offline {
if globalLoggingEnabled {
logMsg(logMessage{Endpoint: b.endpoint, Status: "down", Error: err})
}
b.setOffline()
}

writeErrorResponse(w, r, err)
}

// registerMetricsRouter - add handler functions for metrics.
Expand Down Expand Up @@ -401,7 +416,7 @@ func (m *multisite) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
}
w.WriteHeader(http.StatusBadGateway)
writeErrorResponse(w, r, errors.New("all backend servers are offline"))
}

type site struct {
Expand Down
Loading