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

impactcn 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 a99e5dbf5 [type: refactor] optimize max memory config logic. (#3675)
a99e5dbf5 is described below

commit a99e5dbf5e14ac34d9c0c162cdcb9a0949bc82fb
Author: Qicz <[email protected]>
AuthorDate: Thu Jul 7 15:04:21 2022 +0800

    [type: refactor] optimize max memory config logic. (#3675)
    
    * [type: refactor] optimize max memory config logic.
    
    * code polish
    
    * refactor default maxInMemorySize to 1mb
---
 shenyu-bootstrap/src/main/resources/application.yml     |  5 ++---
 .../org/apache/shenyu/common/config/ShenyuConfig.java   |  5 ++++-
 .../apache/shenyu/plugin/base/AbstractShenyuPlugin.java |  8 ++++++--
 .../plugin/httpclient/config/HttpClientProperties.java  |  8 ++++----
 .../httpclient/HttpClientPluginConfiguration.java       | 17 ++++++-----------
 5 files changed, 22 insertions(+), 21 deletions(-)

diff --git a/shenyu-bootstrap/src/main/resources/application.yml 
b/shenyu-bootstrap/src/main/resources/application.yml
index b396fbb39..86ee65c91 100644
--- a/shenyu-bootstrap/src/main/resources/application.yml
+++ b/shenyu-bootstrap/src/main/resources/application.yml
@@ -72,8 +72,7 @@ management:
 shenyu:
   matchCache:
     enabled: true
-    # 256 * 1024 * 1024 = 256MB
-    maxFreeMemory: 268435456
+    maxFreeMemory: 256 # 256MB
   netty:
     http:
       # set to false, user can custom the netty tcp server config.
@@ -121,7 +120,7 @@ shenyu:
 #    writeTimeout: 3000
 #    wiretap: false
 #    keepAlive: false
-#    maxInMemorySize: 16 * 1024 * 1024
+#    maxInMemorySize: 1 #1mb
 #    pool:
 #      type: ELASTIC
 #      name: proxy
diff --git 
a/shenyu-common/src/main/java/org/apache/shenyu/common/config/ShenyuConfig.java 
b/shenyu-common/src/main/java/org/apache/shenyu/common/config/ShenyuConfig.java
index a133edb32..435f8c927 100644
--- 
a/shenyu-common/src/main/java/org/apache/shenyu/common/config/ShenyuConfig.java
+++ 
b/shenyu-common/src/main/java/org/apache/shenyu/common/config/ShenyuConfig.java
@@ -533,7 +533,10 @@ public class ShenyuConfig {
 
         private boolean enabled;
 
-        private Integer maxFreeMemory = 256 * 1024 * 1024;
+        /**
+         * Max free memory, unit mb.
+         */
+        private Integer maxFreeMemory = 256;
 
         /**
          * Gets enabled.
diff --git 
a/shenyu-plugin/shenyu-plugin-base/src/main/java/org/apache/shenyu/plugin/base/AbstractShenyuPlugin.java
 
b/shenyu-plugin/shenyu-plugin-base/src/main/java/org/apache/shenyu/plugin/base/AbstractShenyuPlugin.java
index ff2e62e44..faf848b6f 100644
--- 
a/shenyu-plugin/shenyu-plugin-base/src/main/java/org/apache/shenyu/plugin/base/AbstractShenyuPlugin.java
+++ 
b/shenyu-plugin/shenyu-plugin-base/src/main/java/org/apache/shenyu/plugin/base/AbstractShenyuPlugin.java
@@ -136,19 +136,23 @@ public abstract class AbstractShenyuPlugin implements 
ShenyuPlugin {
     private void cacheSelectorDataIfEnabled(final String path, final 
SelectorData selectorData) {
         if (matchCacheConfig.getEnabled()) {
             if (Objects.isNull(selectorData)) {
-                MatchDataCache.getInstance().cacheSelectorData(path, 
defaultSelectorData, matchCacheConfig.getMaxFreeMemory());
+                MatchDataCache.getInstance().cacheSelectorData(path, 
defaultSelectorData, getMaxFreeMemory());
             } else {
                 List<ConditionData> conditionList = 
selectorData.getConditionList();
                 if (CollectionUtils.isNotEmpty(conditionList)) {
                     boolean isUriCondition = conditionList.stream().allMatch(v 
-> URI_CONDITION_TYPE.equals(v.getParamType()));
                     if (isUriCondition) {
-                        MatchDataCache.getInstance().cacheSelectorData(path, 
selectorData, matchCacheConfig.getMaxFreeMemory());
+                        MatchDataCache.getInstance().cacheSelectorData(path, 
selectorData, getMaxFreeMemory());
                     }
                 }
             }
         }
     }
 
+    private Integer getMaxFreeMemory() {
+        return matchCacheConfig.getMaxFreeMemory() * 1024 * 1024;
+    }
+
     private Pair<Boolean, SelectorData> obtainSelectorDataCacheIfEnabled(final 
ServerWebExchange exchange) {
         if (matchCacheConfig.getEnabled()) {
             SelectorData selectorData = 
MatchDataCache.getInstance().obtainSelectorData(named(), 
exchange.getRequest().getURI().getPath());
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 606c25275..f36092b50 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
@@ -117,9 +117,9 @@ public class HttpClientProperties {
     private boolean keepAlive;
 
     /**
-     * body max memory size, unit byte.
+     * body max memory size, unit mb.
      */
-    private int maxInMemorySize;
+    private Integer maxInMemorySize = 1;
 
     /**
      * Gets strategy.
@@ -378,7 +378,7 @@ public class HttpClientProperties {
      * get maxInMemorySize.
      * @return maxInMemorySize
      */
-    public int getMaxInMemorySize() {
+    public Integer getMaxInMemorySize() {
         return maxInMemorySize;
     }
 
@@ -386,7 +386,7 @@ public class HttpClientProperties {
      * set maxInMemorySize.
      * @param maxInMemorySize maxInMemorySize
      */
-    public void setMaxInMemorySize(final int maxInMemorySize) {
+    public void setMaxInMemorySize(final Integer maxInMemorySize) {
         this.maxInMemorySize = maxInMemorySize;
     }
 
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 1a539bcc0..111812113 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
@@ -237,17 +237,12 @@ public class HttpClientPluginConfiguration {
         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
+            WebClient webClient = WebClient.builder()
+                    // 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(ExchangeStrategies.builder()
+                            .codecs(codecs -> 
codecs.defaultCodecs().maxInMemorySize(properties.getMaxInMemorySize() * 1024 * 
1024))
+                            .build())
                     .clientConnector(new 
ReactorClientHttpConnector(Objects.requireNonNull(httpClient.getIfAvailable())))
                     .build();
             return new WebClientPlugin(webClient);

Reply via email to