all: return 503 http error if service is temporarily unavailable

Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/156
This commit is contained in:
Aliaksandr Valialkin
2019-08-23 09:46:45 +03:00
parent 8c03a8c4b4
commit ac004665b5
4 changed files with 45 additions and 5 deletions

View File

@@ -423,7 +423,29 @@ var (
func Errorf(w http.ResponseWriter, format string, args ...interface{}) {
errStr := fmt.Sprintf(format, args...)
logger.Errorf("%s", errStr)
http.Error(w, errStr, http.StatusBadRequest)
// Extract statusCode from args
statusCode := http.StatusBadRequest
for _, arg := range args {
if esc, ok := arg.(*ErrorWithStatusCode); ok {
statusCode = esc.StatusCode
break
}
}
http.Error(w, errStr, statusCode)
}
// ErrorWithStatusCode is error with HTTP status code.
//
// The given StatusCode is sent to client when the error is passed to Errorf.
type ErrorWithStatusCode struct {
Err error
StatusCode int
}
// Error implements error interface.
func (e *ErrorWithStatusCode) Error() string {
return e.Err.Error()
}
func isTrivialNetworkError(err error) bool {