EronWright closed pull request #4765: [FLINK-7753] [flip-6] close REST channel 
on server error
URL: https://github.com/apache/flink/pull/4765
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/flink-runtime/src/main/java/org/apache/flink/runtime/rest/handler/util/HandlerUtils.java
 
b/flink-runtime/src/main/java/org/apache/flink/runtime/rest/handler/util/HandlerUtils.java
index 0d7483aad3c..9de150d9dc4 100644
--- 
a/flink-runtime/src/main/java/org/apache/flink/runtime/rest/handler/util/HandlerUtils.java
+++ 
b/flink-runtime/src/main/java/org/apache/flink/runtime/rest/handler/util/HandlerUtils.java
@@ -74,7 +74,7 @@
                        sendErrorResponse(channelHandlerContext, httpRequest, 
new ErrorResponseBody("Internal server error. Could not map response to 
JSON."), HttpResponseStatus.INTERNAL_SERVER_ERROR);
                        return;
                }
-               sendResponse(channelHandlerContext, httpRequest, sw.toString(), 
statusCode);
+               sendResponse(channelHandlerContext, httpRequest, sw.toString(), 
statusCode, false);
        }
 
        /**
@@ -96,9 +96,9 @@ public static void sendErrorResponse(
                        mapper.writeValue(sw, errorMessage);
                } catch (IOException e) {
                        // this should never happen
-                       sendResponse(channelHandlerContext, httpRequest, 
"Internal server error. Could not map error response to JSON.", 
HttpResponseStatus.INTERNAL_SERVER_ERROR);
+                       sendResponse(channelHandlerContext, httpRequest, 
"Internal server error. Could not map error response to JSON.", 
HttpResponseStatus.INTERNAL_SERVER_ERROR, true);
                }
-               sendResponse(channelHandlerContext, httpRequest, sw.toString(), 
statusCode);
+               sendResponse(channelHandlerContext, httpRequest, sw.toString(), 
statusCode, isServerError(statusCode));
        }
 
        /**
@@ -108,19 +108,25 @@ public static void sendErrorResponse(
         * @param httpRequest originating http request
         * @param message which should be sent
         * @param statusCode of the message to send
+        * @param forceClose indicates whether to forcibly close the connection 
after the response is sent
         */
        public static void sendResponse(
                        @Nonnull ChannelHandlerContext channelHandlerContext,
                        @Nonnull HttpRequest httpRequest,
                        @Nonnull String message,
-                       @Nonnull HttpResponseStatus statusCode) {
+                       @Nonnull HttpResponseStatus statusCode,
+                       boolean forceClose) {
                HttpResponse response = new DefaultHttpResponse(HTTP_1_1, 
statusCode);
 
                response.headers().set(CONTENT_TYPE, "application/json");
 
-               if (HttpHeaders.isKeepAlive(httpRequest)) {
+               boolean keepAlive = !forceClose && 
HttpHeaders.isKeepAlive(httpRequest);
+               if (keepAlive) {
                        response.headers().set(CONNECTION, 
HttpHeaders.Values.KEEP_ALIVE);
                }
+               else {
+                       response.headers().set(CONNECTION, 
HttpHeaders.Values.CLOSE);
+               }
 
                byte[] buf = message.getBytes(ConfigConstants.DEFAULT_CHARSET);
                ByteBuf b = Unpooled.copiedBuffer(buf);
@@ -134,8 +140,12 @@ public static void sendResponse(
                ChannelFuture lastContentFuture = 
channelHandlerContext.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
 
                // close the connection, if no keep-alive is needed
-               if (!HttpHeaders.isKeepAlive(httpRequest)) {
+               if (!keepAlive) {
                        
lastContentFuture.addListener(ChannelFutureListener.CLOSE);
                }
        }
+
+       private static boolean isServerError(@Nonnull HttpResponseStatus 
statusCode) {
+               return statusCode.code() >= 500 && statusCode.code() < 600;
+       }
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to