You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by zh...@apache.org on 2022/10/12 02:24:42 UTC

[shenyu] branch master updated: [ISSUE #4059] optimize timeout response (#4070)

This is an automated email from the ASF dual-hosted git repository.

zhangzicheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new b5c18fd20 [ISSUE #4059] optimize timeout response (#4070)
b5c18fd20 is described below

commit b5c18fd20a389dbf210625b15e2730f5b509e2af
Author: moremind <he...@hotmail.com>
AuthorDate: Wed Oct 12 10:24:30 2022 +0800

    [ISSUE #4059] optimize timeout response (#4070)
---
 .../httpclient/AbstractHttpClientPlugin.java       |  9 +++-
 .../exception/ShenyuTimeoutException.java          | 55 ++++++++++++++++++++++
 .../shenyu/web/handler/GlobalErrorHandler.java     |  7 ++-
 3 files changed, 69 insertions(+), 2 deletions(-)

diff --git a/shenyu-plugin/shenyu-plugin-httpclient/src/main/java/org/apache/shenyu/plugin/httpclient/AbstractHttpClientPlugin.java b/shenyu-plugin/shenyu-plugin-httpclient/src/main/java/org/apache/shenyu/plugin/httpclient/AbstractHttpClientPlugin.java
index 86a1f701a..2d15b6ef7 100644
--- a/shenyu-plugin/shenyu-plugin-httpclient/src/main/java/org/apache/shenyu/plugin/httpclient/AbstractHttpClientPlugin.java
+++ b/shenyu-plugin/shenyu-plugin-httpclient/src/main/java/org/apache/shenyu/plugin/httpclient/AbstractHttpClientPlugin.java
@@ -34,6 +34,7 @@ import org.apache.shenyu.plugin.api.result.ShenyuResultEnum;
 import org.apache.shenyu.plugin.api.result.ShenyuResultWrap;
 import org.apache.shenyu.plugin.api.utils.RequestUrlUtils;
 import org.apache.shenyu.plugin.api.utils.WebFluxResultUtils;
+import org.apache.shenyu.plugin.httpclient.exception.ShenyuTimeoutException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.core.io.buffer.DataBuffer;
@@ -89,13 +90,19 @@ public abstract class AbstractHttpClientPlugin<R> implements ShenyuPlugin {
                     .transientErrors(true)
                     .jitter(0.5d)
                     .filter(t -> t instanceof TimeoutException || t instanceof ConnectTimeoutException
-                            || t instanceof ReadTimeoutException || t instanceof IllegalStateException);
+                            || t instanceof ReadTimeoutException || t instanceof IllegalStateException)
+                    .onRetryExhaustedThrow((retryBackoffSpecErr, retrySignal) -> {
+                        throw new ShenyuTimeoutException("Request timeout, the maximum number of retry times has been exceeded");
+                    });
             return response.retryWhen(retryBackoffSpec)
+                    .onErrorMap(ShenyuTimeoutException.class, th -> new ResponseStatusException(HttpStatus.REQUEST_TIMEOUT, th.getMessage(), th))
                     .onErrorMap(TimeoutException.class, th -> new ResponseStatusException(HttpStatus.GATEWAY_TIMEOUT, th.getMessage(), th))
                     .flatMap((Function<Object, Mono<? extends Void>>) o -> chain.execute(exchange));
         }
         final Set<URI> exclude = Sets.newHashSet(uri);
         return resend(response, exchange, duration, httpHeaders, exclude, retryTimes)
+                .onErrorMap(ShenyuException.class, th -> new ResponseStatusException(HttpStatus.SERVICE_UNAVAILABLE,
+                        ShenyuResultEnum.CANNOT_FIND_HEALTHY_UPSTREAM_URL_AFTER_FAILOVER.getMsg(), th))
                 .onErrorMap(TimeoutException.class, th -> new ResponseStatusException(HttpStatus.GATEWAY_TIMEOUT, th.getMessage(), th))
                 .flatMap((Function<Object, Mono<? extends Void>>) o -> chain.execute(exchange));
     }
diff --git a/shenyu-plugin/shenyu-plugin-httpclient/src/main/java/org/apache/shenyu/plugin/httpclient/exception/ShenyuTimeoutException.java b/shenyu-plugin/shenyu-plugin-httpclient/src/main/java/org/apache/shenyu/plugin/httpclient/exception/ShenyuTimeoutException.java
new file mode 100644
index 000000000..4028cd126
--- /dev/null
+++ b/shenyu-plugin/shenyu-plugin-httpclient/src/main/java/org/apache/shenyu/plugin/httpclient/exception/ShenyuTimeoutException.java
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.plugin.httpclient.exception;
+
+
+/**
+ * Shenyu request timeout exception.
+ */
+public final class ShenyuTimeoutException extends RuntimeException {
+
+    private static final long serialVersionUID = -6123954847415409614L;
+
+    /**
+     * Instantiates a new Shenyu request timeout exception.
+     *
+     * @param e the e
+     */
+    public ShenyuTimeoutException(final Throwable e) {
+        super(e);
+    }
+
+    /**
+     * Instantiates a new Shenyu request timeout exception.
+     *
+     * @param message the message
+     */
+    public ShenyuTimeoutException(final String message) {
+        super(message);
+    }
+
+    /**
+     * Instantiates a new Shenyu request timeout exception.
+     *
+     * @param message   the message
+     * @param throwable the throwable
+     */
+    public ShenyuTimeoutException(final String message, final Throwable throwable) {
+        super(message, throwable);
+    }
+}
diff --git a/shenyu-web/src/main/java/org/apache/shenyu/web/handler/GlobalErrorHandler.java b/shenyu-web/src/main/java/org/apache/shenyu/web/handler/GlobalErrorHandler.java
index 7900bdc63..7cb053144 100644
--- a/shenyu-web/src/main/java/org/apache/shenyu/web/handler/GlobalErrorHandler.java
+++ b/shenyu-web/src/main/java/org/apache/shenyu/web/handler/GlobalErrorHandler.java
@@ -25,6 +25,7 @@ import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.server.reactive.ServerHttpRequest;
 import org.springframework.lang.NonNull;
+import org.springframework.util.StringUtils;
 import org.springframework.web.server.ResponseStatusException;
 import org.springframework.web.server.ServerWebExchange;
 import reactor.core.publisher.Mono;
@@ -51,11 +52,15 @@ public class GlobalErrorHandler implements ErrorWebExceptionHandler {
     public Mono<Void> handle(@NonNull final ServerWebExchange exchange, @NonNull final Throwable throwable) {
         LOG.error("handle error: {}{}", exchange.getLogPrefix(), formatError(throwable, exchange.getRequest()), throwable);
         HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
+        String errMsg = httpStatus.getReasonPhrase();
         if (throwable instanceof ResponseStatusException) {
             httpStatus = ((ResponseStatusException) throwable).getStatus();
+            if (StringUtils.hasLength(((ResponseStatusException) throwable).getReason())) {
+                errMsg = ((ResponseStatusException) throwable).getReason();
+            }
         }
         exchange.getResponse().setStatusCode(httpStatus);
-        Object error = ShenyuResultWrap.error(exchange, httpStatus.value(), httpStatus.getReasonPhrase(), throwable);
+        Object error = ShenyuResultWrap.error(exchange, httpStatus.value(), errMsg, throwable);
         return WebFluxResultUtils.result(exchange, error);
     }