You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by he...@apache.org on 2023/12/02 04:11:15 UTC

(shenyu) branch master updated: Some code optimizations:removing duplicate code and extracting the same code for common use. (#5336)

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

hefengen 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 ec26dc0b96 Some code optimizations:removing duplicate code and extracting the same code for common use. (#5336)
ec26dc0b96 is described below

commit ec26dc0b968d8142006b34cbcc0672b30d6d6481
Author: Junwei Lian <13...@users.noreply.github.com>
AuthorDate: Sat Dec 2 12:11:10 2023 +0800

    Some code optimizations:removing duplicate code and extracting the same code for common use. (#5336)
    
    * apidoc sql
    
    * refact
    
    * commit
    
    * [Task] Shenyu-admin: Fix API document failed to build because of NPE.
    
    * [Task] Shenyu-admin: Fix API document failed to build because of NPE.
    
    * solve conficts,modify LICENSE.
    
    * delete useless code.
    
    * delete useless code.
    
    * commit
    
    * [ISSUE #3843]admin apidoc fix: the required attribute prompt is incorrect when micro service parameter uses "@ApiModelProperty".
    
    * commit
    
    * [shenyu-examples]add swagger to the example project to test the apidoc function of the gateway management system.
    
    * commit
    
    * commit
    
    * commit
    
    * [ISSUE #4690]Supports gzip compression in response to HTTP requests.
    
    * [examples]Add Swagger sample project to demonstrate automatic pull interface documentation.
    
    * delete exapmple
    
    * delete useless code.
    
    * delete useless code.
    
    * Some code optimizations.
    
    ---------
    
    Co-authored-by: lianjunwei <li...@didiglobal.com>
    Co-authored-by: dragon-zhang <zh...@apache.org>
---
 .../apache/shenyu/admin/config/properties/DashboardProperties.java | 3 ++-
 .../org/apache/shenyu/admin/config/properties/JwtProperties.java   | 3 ++-
 .../shenyu/admin/service/impl/OperationRecordLogServiceImpl.java   | 6 +++---
 .../org/apache/shenyu/admin/service/impl/PluginServiceImpl.java    | 6 ++++--
 .../java/org/apache/shenyu/common/constant/AdminConstants.java     | 2 ++
 .../src/main/java/org/apache/shenyu/common/constant/Constants.java | 4 +++-
 .../org/apache/shenyu/plugin/grpc/client/GrpcClientBuilder.java    | 2 +-
 .../starter/plugin/httpclient/HttpClientPluginConfiguration.java   | 3 ++-
 .../springboot/plugin/websocket/WebSocketPluginConfiguration.java  | 3 ++-
 .../src/main/java/org/apache/shenyu/web/filter/FileSizeFilter.java | 7 +++----
 10 files changed, 24 insertions(+), 15 deletions(-)

diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/config/properties/DashboardProperties.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/config/properties/DashboardProperties.java
index d75d77b903..ece895d0e5 100644
--- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/config/properties/DashboardProperties.java
+++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/config/properties/DashboardProperties.java
@@ -18,6 +18,7 @@
 package org.apache.shenyu.admin.config.properties;
 
 import org.apache.commons.collections4.CollectionUtils;
+import org.apache.shenyu.common.constant.AdminConstants;
 import org.springframework.beans.factory.InitializingBean;
 import org.springframework.boot.context.properties.ConfigurationProperties;
 import org.springframework.context.annotation.Configuration;
@@ -68,7 +69,7 @@ public class DashboardProperties implements InitializingBean {
      * enable SuperAdminPassword valid duration.
      * default is 30 day
      */
-    private Long superAdminPasswordValidDuration = 30 * 24 * 60 * 60 * 1000L;
+    private Long superAdminPasswordValidDuration = 30 * AdminConstants.THE_ONE_DAY_MILLIS_TIME;
     
     
     /**
diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/config/properties/JwtProperties.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/config/properties/JwtProperties.java
index c86b6d5125..3eabd2ae18 100644
--- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/config/properties/JwtProperties.java
+++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/config/properties/JwtProperties.java
@@ -17,6 +17,7 @@
 
 package org.apache.shenyu.admin.config.properties;
 
+import org.apache.shenyu.common.constant.AdminConstants;
 import org.springframework.boot.context.properties.ConfigurationProperties;
 import org.springframework.stereotype.Component;
 
@@ -27,7 +28,7 @@ import org.springframework.stereotype.Component;
 @ConfigurationProperties(prefix = "shenyu.jwt")
 public class JwtProperties {
 
-    private Long expiredSeconds = 24 * 60 * 60 * 1000L;
+    private Long expiredSeconds = AdminConstants.THE_ONE_DAY_MILLIS_TIME;
 
     /**
      * Gets the value of expiredSeconds.
diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/OperationRecordLogServiceImpl.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/OperationRecordLogServiceImpl.java
index 7c5de58558..e13a8cffeb 100644
--- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/OperationRecordLogServiceImpl.java
+++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/OperationRecordLogServiceImpl.java
@@ -50,7 +50,7 @@ public class OperationRecordLogServiceImpl implements OperationRecordLogService
     public void doConditionPreProcessing(final RecordLogQueryCondition condition) {
         condition.init();
         Assert.isTrue(condition.getEndTime().getTime() > condition.getStartTime().getTime(), "end time must be greater than start time");
-        if (!AdminConstants.ADMIN_NAME.equals(SessionUtil.visitorName())) {
+        if (!SessionUtil.isAdmin()) {
             condition.setUsername(SessionUtil.visitorName());
         }
     }
@@ -62,7 +62,7 @@ public class OperationRecordLogServiceImpl implements OperationRecordLogService
     
     @Override
     public List<OperationRecordLog> list() {
-        if (AdminConstants.ADMIN_NAME.equals(SessionUtil.visitorName())) {
+        if (SessionUtil.isAdmin()) {
             return recordLogMapper.selectLimit(null, dashboardProperties.getRecordLogLimit());
         }
         return recordLogMapper.selectLimit(SessionUtil.visitorName(), dashboardProperties.getRecordLogLimit());
@@ -70,7 +70,7 @@ public class OperationRecordLogServiceImpl implements OperationRecordLogService
     
     @Override
     public boolean cleanHistory(final Date date) {
-        final long supportMaxTime = System.currentTimeMillis() - (dashboardProperties.getOnlyCleanDays() * 1000 * 60 * 60 * 24);
+        final long supportMaxTime = System.currentTimeMillis() - (dashboardProperties.getOnlyCleanDays() * AdminConstants.THE_ONE_DAY_MILLIS_TIME);
         Assert.isTrue(date.getTime() < supportMaxTime, String.format("Only supports cleaning data older than %d days", dashboardProperties.getOnlyCleanDays()));
         return recordLogMapper.deleteByBefore(date) > 0;
     }
diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/PluginServiceImpl.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/PluginServiceImpl.java
index fe85d44a4f..07c4507d3e 100644
--- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/PluginServiceImpl.java
+++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/PluginServiceImpl.java
@@ -34,6 +34,7 @@ import org.apache.shenyu.admin.service.PluginService;
 import org.apache.shenyu.admin.service.publish.PluginEventPublisher;
 import org.apache.shenyu.admin.transfer.PluginTransfer;
 import org.apache.shenyu.admin.utils.Assert;
+import org.apache.shenyu.common.constant.Constants;
 import org.apache.shenyu.common.utils.ListUtil;
 import org.apache.shenyu.admin.utils.SessionUtil;
 import org.apache.shenyu.admin.utils.ShenyuResultMessage;
@@ -209,7 +210,7 @@ public class PluginServiceImpl implements PluginService {
     
     @Override
     public List<PluginSnapshotVO> activePluginSnapshot() {
-        return pluginMapper.activePluginSnapshot(AdminConstants.ADMIN_NAME.equals(SessionUtil.visitorName()) ? null : SessionUtil.visitor().getUserId());
+        return pluginMapper.activePluginSnapshot(SessionUtil.isAdmin() ? null : SessionUtil.visitor().getUserId());
     }
     
     /**
@@ -257,12 +258,13 @@ public class PluginServiceImpl implements PluginService {
 
     /**
      * check jar.
+     *
      * @param file jar file
      * @return true is right
      */
     private boolean checkFile(final MultipartFile file) {
         try {
-            if (file.getSize() > 16 * 1024 * 1024) {
+            if (file.getSize() > 16 * Constants.BYTES_PER_MB) {
                 return false;
             }
             Set<String> dependencyTree = JarDependencyUtils.getDependencyTree(file.getBytes());
diff --git a/shenyu-common/src/main/java/org/apache/shenyu/common/constant/AdminConstants.java b/shenyu-common/src/main/java/org/apache/shenyu/common/constant/AdminConstants.java
index 472b9f7736..5db18edb8a 100644
--- a/shenyu-common/src/main/java/org/apache/shenyu/common/constant/AdminConstants.java
+++ b/shenyu-common/src/main/java/org/apache/shenyu/common/constant/AdminConstants.java
@@ -273,5 +273,7 @@ public final class AdminConstants {
     public static final String PROXY_SELECTOR_NAME_IS_EXIST = "The proxy selector name already exists and can't be added repeatedly!";
 
     public static final String PROXY_SELECTOR_ID_IS_NOT_EXIST = "The proxy selector(s) does not exist";
+
+    public static final long THE_ONE_DAY_MILLIS_TIME = 24 * 60 * 60 * 1000L;
 }
 
diff --git a/shenyu-common/src/main/java/org/apache/shenyu/common/constant/Constants.java b/shenyu-common/src/main/java/org/apache/shenyu/common/constant/Constants.java
index 8024b71fe6..8cb9910056 100644
--- a/shenyu-common/src/main/java/org/apache/shenyu/common/constant/Constants.java
+++ b/shenyu-common/src/main/java/org/apache/shenyu/common/constant/Constants.java
@@ -711,7 +711,9 @@ public interface Constants {
      * The maximum free memory reserved by the blocking queue for the JVM.
      */
     int THE_256_MB = 256 * 1024 * 1024;
-    
+
+    int BYTES_PER_MB = 1024 * 1024;
+
     /**
      * The default cluster of dubbo client.
      */
diff --git a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-grpc/src/main/java/org/apache/shenyu/plugin/grpc/client/GrpcClientBuilder.java b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-grpc/src/main/java/org/apache/shenyu/plugin/grpc/client/GrpcClientBuilder.java
index 734bcae1e3..a0074c9ac7 100644
--- a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-grpc/src/main/java/org/apache/shenyu/plugin/grpc/client/GrpcClientBuilder.java
+++ b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-grpc/src/main/java/org/apache/shenyu/plugin/grpc/client/GrpcClientBuilder.java
@@ -67,7 +67,7 @@ public final class GrpcClientBuilder {
                 .intercept(new ContextClientInterceptor())
                 .defaultLoadBalancingPolicy(LoadBalancerStrategy.SHENYU.getStrategy())
                 .usePlaintext()
-                .maxInboundMessageSize(100 * 1024 * 1024)
+                .maxInboundMessageSize(100 * Constants.BYTES_PER_MB)
                 .executor(buildExecutor())
                 .disableRetry();
         ManagedChannel channel = builder.build();
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 0c72ea97ba..c889a90e3a 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
@@ -17,6 +17,7 @@
 
 package org.apache.shenyu.springboot.starter.plugin.httpclient;
 
+import org.apache.shenyu.common.constant.Constants;
 import org.apache.shenyu.plugin.api.ShenyuPlugin;
 import org.apache.shenyu.plugin.httpclient.NettyHttpClientPlugin;
 import org.apache.shenyu.plugin.httpclient.WebClientPlugin;
@@ -118,7 +119,7 @@ public class HttpClientPluginConfiguration {
                     // 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))
+                            .codecs(codecs -> codecs.defaultCodecs().maxInMemorySize(properties.getMaxInMemorySize() * Constants.BYTES_PER_MB))
                             .build())
                     .clientConnector(new ReactorClientHttpConnector(Objects.requireNonNull(httpClient.getIfAvailable())))
                     .build();
diff --git a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-websocket/src/main/java/org/apache/shenyu/springboot/plugin/websocket/WebSocketPluginConfiguration.java b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-websocket/src/main/java/org/apache/shenyu/springboot/plugin/websocket/WebSocketPluginConfiguration.java
index 4e9d8fed44..2a7fcf36d2 100644
--- a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-websocket/src/main/java/org/apache/shenyu/springboot/plugin/websocket/WebSocketPluginConfiguration.java
+++ b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-websocket/src/main/java/org/apache/shenyu/springboot/plugin/websocket/WebSocketPluginConfiguration.java
@@ -18,6 +18,7 @@
 package org.apache.shenyu.springboot.plugin.websocket;
 
 import org.apache.shenyu.common.config.ShenyuConfig;
+import org.apache.shenyu.common.constant.Constants;
 import org.apache.shenyu.plugin.api.context.ShenyuContextDecorator;
 import org.apache.shenyu.plugin.base.handler.DiscoveryUpstreamDataHandler;
 import org.apache.shenyu.plugin.base.handler.PluginDataHandler;
@@ -78,7 +79,7 @@ public class WebSocketPluginConfiguration {
     public ReactorNettyWebSocketClient reactorNettyWebSocketClient(final ShenyuConfig shenyuConfig,
                                                                    final ObjectProvider<HttpClient> httpClient) {
         Supplier<WebsocketClientSpec.Builder> builder = WebsocketClientSpec.builder()
-                .maxFramePayloadLength(shenyuConfig.getWebsocket().getMaxFramePayloadSize() * 1024 * 1024)
+                .maxFramePayloadLength(shenyuConfig.getWebsocket().getMaxFramePayloadSize() * Constants.BYTES_PER_MB)
                 .handlePing(shenyuConfig.getWebsocket().getEnableProxyPing());
         return new ReactorNettyWebSocketClient(httpClient.getIfAvailable(HttpClient::create), builder);
     }
diff --git a/shenyu-web/src/main/java/org/apache/shenyu/web/filter/FileSizeFilter.java b/shenyu-web/src/main/java/org/apache/shenyu/web/filter/FileSizeFilter.java
index 256a6f4a91..82d6c88afd 100644
--- a/shenyu-web/src/main/java/org/apache/shenyu/web/filter/FileSizeFilter.java
+++ b/shenyu-web/src/main/java/org/apache/shenyu/web/filter/FileSizeFilter.java
@@ -17,6 +17,7 @@
 
 package org.apache.shenyu.web.filter;
 
+import org.apache.shenyu.common.constant.Constants;
 import org.apache.shenyu.plugin.api.result.ShenyuResultEnum;
 import org.apache.shenyu.plugin.api.result.ShenyuResultWrap;
 import org.apache.shenyu.plugin.api.utils.WebFluxResultUtils;
@@ -55,8 +56,6 @@ import java.util.function.Function;
 public class FileSizeFilter implements WebFilter {
     private static final Logger LOG = LoggerFactory.getLogger(FileSizeFilter.class);
 
-    private static final int BYTES_PER_MB = 1024 * 1024;
-
     private final int fileMaxSize;
 
     private final List<HttpMessageReader<?>> messageReaders;
@@ -76,11 +75,11 @@ public class FileSizeFilter implements WebFilter {
                     messageReaders);
             return serverRequest.bodyToMono(DataBuffer.class)
                     .flatMap(dataBuffer -> {
-                        if (dataBuffer.capacity() > BYTES_PER_MB * fileMaxSize) {
+                        if (dataBuffer.capacity() > Constants.BYTES_PER_MB * fileMaxSize) {
                             ServerHttpResponse response = exchange.getResponse();
                             response.setStatusCode(HttpStatus.BAD_REQUEST);
                             Object error = ShenyuResultWrap.error(exchange, ShenyuResultEnum.PAYLOAD_TOO_LARGE);
-                            LOG.info("The file size exceeds the limit. The actual size is {}M , response:{}", dataBuffer.capacity() / BYTES_PER_MB, error);
+                            LOG.info("The file size exceeds the limit. The actual size is {}M , response:{}", dataBuffer.capacity() / Constants.BYTES_PER_MB, error);
                             return WebFluxResultUtils.result(exchange, error);
                         }
                         BodyInserter<Mono<DataBuffer>, ReactiveHttpOutputMessage> bodyInsert = BodyInserters.fromPublisher(Mono.just(dataBuffer), DataBuffer.class);