You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by xi...@apache.org on 2022/07/03 08:53:31 UTC

[incubator-shenyu] branch master updated: [type: feat] body maxInMemorySize (#3640)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 5bee11232 [type: feat] body maxInMemorySize (#3640)
5bee11232 is described below

commit 5bee11232b27f52e02048726126316c155fec8c8
Author: Sinsy <55...@qq.com>
AuthorDate: Sun Jul 3 16:53:26 2022 +0800

    [type: feat] body maxInMemorySize (#3640)
    
    * "feat body maxInMemorySize"
    
    * fix ci error
    
    * fix ci error
---
 shenyu-bootstrap/src/main/resources/application.yml |  1 +
 .../httpclient/config/HttpClientProperties.java     | 21 +++++++++++++++++++++
 .../httpclient/HttpClientPluginConfiguration.java   | 17 +++++++++++++++--
 3 files changed, 37 insertions(+), 2 deletions(-)

diff --git a/shenyu-bootstrap/src/main/resources/application.yml b/shenyu-bootstrap/src/main/resources/application.yml
index 6a751d6e9..e2627619a 100644
--- a/shenyu-bootstrap/src/main/resources/application.yml
+++ b/shenyu-bootstrap/src/main/resources/application.yml
@@ -121,6 +121,7 @@ shenyu:
 #    writeTimeout: 3000
 #    wiretap: false
 #    keepAlive: false
+#    maxInMemorySize: 16 * 1024 * 1024
 #    pool:
 #      type: ELASTIC
 #      name: proxy
diff --git a/shenyu-plugin/shenyu-plugin-httpclient/src/main/java/org/apache/shenyu/plugin/httpclient/config/HttpClientProperties.java b/shenyu-plugin/shenyu-plugin-httpclient/src/main/java/org/apache/shenyu/plugin/httpclient/config/HttpClientProperties.java
index d52bb28fb..606c25275 100644
--- a/shenyu-plugin/shenyu-plugin-httpclient/src/main/java/org/apache/shenyu/plugin/httpclient/config/HttpClientProperties.java
+++ b/shenyu-plugin/shenyu-plugin-httpclient/src/main/java/org/apache/shenyu/plugin/httpclient/config/HttpClientProperties.java
@@ -116,6 +116,11 @@ public class HttpClientProperties {
      */
     private boolean keepAlive;
 
+    /**
+     * body max memory size, unit byte.
+     */
+    private int maxInMemorySize;
+
     /**
      * Gets strategy.
      *
@@ -369,6 +374,22 @@ public class HttpClientProperties {
         this.keepAlive = keepAlive;
     }
 
+    /**
+     * get maxInMemorySize.
+     * @return maxInMemorySize
+     */
+    public int getMaxInMemorySize() {
+        return maxInMemorySize;
+    }
+
+    /**
+     * set maxInMemorySize.
+     * @param maxInMemorySize maxInMemorySize
+     */
+    public void setMaxInMemorySize(final int maxInMemorySize) {
+        this.maxInMemorySize = maxInMemorySize;
+    }
+
     /**
      * The type Pool.
      */
diff --git a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-httpclient/src/main/java/org/apache/shenyu/springboot/starter/plugin/httpclient/HttpClientPluginConfiguration.java b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-httpclient/src/main/java/org/apache/shenyu/springboot/starter/plugin/httpclient/HttpClientPluginConfiguration.java
index c522ccb8f..1a539bcc0 100644
--- a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-httpclient/src/main/java/org/apache/shenyu/springboot/starter/plugin/httpclient/HttpClientPluginConfiguration.java
+++ b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-httpclient/src/main/java/org/apache/shenyu/springboot/starter/plugin/httpclient/HttpClientPluginConfiguration.java
@@ -35,6 +35,7 @@ import org.springframework.boot.context.properties.PropertyMapper;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.http.client.reactive.ReactorClientHttpConnector;
+import org.springframework.web.reactive.function.client.ExchangeStrategies;
 import org.springframework.web.reactive.function.client.WebClient;
 import reactor.netty.http.client.HttpClient;
 import reactor.netty.resources.ConnectionProvider;
@@ -233,8 +234,20 @@ public class HttpClientPluginConfiguration {
          * @return the shenyu plugin
          */
         @Bean
-        public ShenyuPlugin webClientPlugin(final ObjectProvider<HttpClient> httpClient) {
-            WebClient webClient = WebClient.builder()
+        public ShenyuPlugin webClientPlugin(
+                final HttpClientProperties properties,
+                final ObjectProvider<HttpClient> httpClient) {
+            WebClient.Builder builder = WebClient.builder();
+            if (properties.getMaxInMemorySize() != 0) {
+                // fix Exceeded limit on max bytes to buffer
+                // detail see https://stackoverflow.com/questions/59326351/configure-spring-codec-max-in-memory-size-when-using-reactiveelasticsearchclient
+                ExchangeStrategies strategies = ExchangeStrategies.builder()
+                        .codecs(codecs -> codecs.defaultCodecs().maxInMemorySize(properties.getMaxInMemorySize()))
+                        .build();
+                builder = builder.exchangeStrategies(strategies);
+            }
+
+            WebClient webClient = builder
                     .clientConnector(new ReactorClientHttpConnector(Objects.requireNonNull(httpClient.getIfAvailable())))
                     .build();
             return new WebClientPlugin(webClient);