Use ErrorHandler to handle proxy error (#1310)
The router prints error no matter what error type it is. It's useful for troubleshooting, however, it also prints the context canceled error, which means that users abort request before reply and its really normal nowadays. Also, the router returns 502 if error is not nil and may confused client if it's a timeout error. To solve these problems, this PR adds an error handler to reverse proxy to examine the return error and change the status code when needed.
This commit is contained in:
@@ -296,12 +296,18 @@ func (roundTripper RetryingRoundTripper) RoundTrip(req *http.Request) (*http.Res
|
||||
if roundTripper.timeout <= 0 {
|
||||
roundTripper.timeout = fv1.DEFAULT_FUNCTION_TIMEOUT
|
||||
}
|
||||
roundTripper.logger.Debug("Creating context for request for ", zap.Any("Time", roundTripper.timeout))
|
||||
ctx, closeCtx := context.WithTimeout(context.Background(), time.Duration(roundTripper.timeout)*time.Second)
|
||||
|
||||
roundTripper.logger.Debug("Creating context for request for ", zap.Any("time", roundTripper.timeout))
|
||||
// pass request context as parent context for the case
|
||||
// that user aborts connection before timeout. Otherwise,
|
||||
// the request won't be canceled until the deadline exceeded
|
||||
// which may be a potential security issue.
|
||||
ctx, closeCtx := context.WithTimeout(req.Context(), time.Duration(roundTripper.timeout)*time.Second)
|
||||
|
||||
// forward the request to the function service
|
||||
resp, err = ocRoundTripper.RoundTrip(req.WithContext(ctx))
|
||||
closeCtx()
|
||||
|
||||
if err == nil {
|
||||
// Track metrics
|
||||
httpMetricLabels.code = resp.StatusCode
|
||||
@@ -348,7 +354,6 @@ func (roundTripper RetryingRoundTripper) RoundTrip(req *http.Request) (*http.Res
|
||||
|
||||
// if transport.RoundTrip returns a non-network dial error (e.g. "context canceled"), then relay it back to user
|
||||
if !isNetDialErr {
|
||||
err = errors.Wrapf(err, "error sending request to function %v", fnMeta.Name)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
@@ -459,6 +464,7 @@ func (fh functionHandler) handler(responseWriter http.ResponseWriter, request *h
|
||||
funcHandler: &fh,
|
||||
timeout: timeout,
|
||||
},
|
||||
ErrorHandler: getProxyErrorHandler(fh.logger, fh.function),
|
||||
}
|
||||
|
||||
proxy.ServeHTTP(responseWriter, request)
|
||||
@@ -499,6 +505,32 @@ func getCanaryBackend(fnMetadatamap map[string]*metav1.ObjectMeta, fnWtDistribut
|
||||
return fnMetadatamap[fnName]
|
||||
}
|
||||
|
||||
// getProxyErrorHandler returns a reverse proxy error handler
|
||||
func getProxyErrorHandler(logger *zap.Logger, fnMeta *metav1.ObjectMeta) func(rw http.ResponseWriter, req *http.Request, err error) {
|
||||
return func(rw http.ResponseWriter, req *http.Request, err error) {
|
||||
status := http.StatusBadGateway
|
||||
switch err {
|
||||
case context.Canceled:
|
||||
// 499 CLIENT CLOSED REQUEST
|
||||
// A non-standard status code introduced by nginx for the case
|
||||
// when a client closes the connection while nginx is processing the request.
|
||||
// Reference: https://httpstatuses.com/499
|
||||
status = 499
|
||||
logger.Debug("client closes the connection",
|
||||
zap.Any("function", fnMeta), zap.Any("request_header", req.Header))
|
||||
case context.DeadlineExceeded:
|
||||
status = http.StatusGatewayTimeout
|
||||
logger.Error("function not responses before the timeout",
|
||||
zap.Any("function", fnMeta), zap.Any("request_header", req.Header))
|
||||
default:
|
||||
logger.Error("error sending request to function",
|
||||
zap.Error(err), zap.Any("function", fnMeta), zap.Any("request_header", req.Header))
|
||||
}
|
||||
// TODO: return error message that contains traceable UUID back to user. Issue #693
|
||||
rw.WriteHeader(status)
|
||||
}
|
||||
}
|
||||
|
||||
// addForwardedHostHeader add "forwarded host" to request header
|
||||
func (roundTripper RetryingRoundTripper) addForwardedHostHeader(req *http.Request) {
|
||||
// for more detailed information, please visit:
|
||||
|
||||
Reference in New Issue
Block a user