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/06/03 08:34:01 UTC

[incubator-shenyu] branch master updated: [type:optimize] optimize netty http server parameters. (#3474)

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 31e4ecc38 [type:optimize] optimize netty http server parameters. (#3474)
31e4ecc38 is described below

commit 31e4ecc38fc34a4ba956a835fe3c92c873ba301f
Author: moremind <he...@hotmail.com>
AuthorDate: Fri Jun 3 16:33:56 2022 +0800

    [type:optimize] optimize netty http server parameters. (#3474)
    
    * [type:optimize] optimize netty http server parameters.
    
    * [type:optimize] optimize netty http server parameters.
    
    * [type:optimize] optimize netty http server parameters.
    
    * [type:optimize] optimize netty http server parameters.
    
    * [type:optimize] optimize netty http server parameters.
    
    * [type:optimize] optimize netty http server parameters.
---
 .../src/main/resources/application.yml             | 11 +++-
 .../starter/netty/NettyChannelProperties.java      | 62 +++++++++++++++++++-
 ...TcpProperties.java => NettyHttpProperties.java} | 22 ++++++-
 .../netty/ShenyuNettyWebServerConfiguration.java   | 67 +++++++++++++++-------
 .../ShenyuNettyWebServerConfigurationTest.java     | 46 ++++++++-------
 5 files changed, 161 insertions(+), 47 deletions(-)

diff --git a/shenyu-bootstrap/src/main/resources/application.yml b/shenyu-bootstrap/src/main/resources/application.yml
index df7b805d1..616f5712f 100644
--- a/shenyu-bootstrap/src/main/resources/application.yml
+++ b/shenyu-bootstrap/src/main/resources/application.yml
@@ -71,11 +71,12 @@ management:
 
 shenyu:
   netty:
-    tcp:
+    http:
       # set to false, user can custom the netty tcp server config.
       webServerFactoryEnabled: true
       selectCount: 1
       workerCount: 4
+      accessLog: false
       serverSocketChannel:
         soRcvBuf: 87380
         soBackLog: 128
@@ -84,8 +85,10 @@ shenyu:
         writeBufferHighWaterMark: 65536
         writeBufferLowWaterMark: 32768
         writeSpinCount: 16
-        autoRead: true
+        autoRead: false
         allocType: "pooled"
+        messageSizeEstimator: 8
+        singleEventExecutorPerGroup: true
       socketChannel:
         soKeepAlive: false
         soReuseAddr: false
@@ -99,8 +102,10 @@ shenyu:
         writeBufferHighWaterMark: 65536
         writeBufferLowWaterMark: 32768
         writeSpinCount: 16
-        autoRead: true
+        autoRead: false
         allocType: "pooled"
+        messageSizeEstimator: 8
+        singleEventExecutorPerGroup: true
 #  httpclient:
 #    strategy: webClient
 #    connectTimeout: 45000
diff --git a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-gateway/src/main/java/org/apache/shenyu/springboot/starter/netty/NettyChannelProperties.java b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-gateway/src/main/java/org/apache/shenyu/springboot/starter/netty/NettyChannelProperties.java
index a9e52dc36..20600f7a7 100644
--- a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-gateway/src/main/java/org/apache/shenyu/springboot/starter/netty/NettyChannelProperties.java
+++ b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-gateway/src/main/java/org/apache/shenyu/springboot/starter/netty/NettyChannelProperties.java
@@ -20,6 +20,10 @@ package org.apache.shenyu.springboot.starter.netty;
 import io.netty.buffer.ByteBufAllocator;
 import io.netty.buffer.PooledByteBufAllocator;
 import io.netty.buffer.UnpooledByteBufAllocator;
+import io.netty.channel.DefaultMessageSizeEstimator;
+import io.netty.channel.MessageSizeEstimator;
+
+import java.util.Objects;
 
 /**
  * Netty channel properties.
@@ -38,7 +42,7 @@ public class NettyChannelProperties {
 
     private Integer writeSpinCount = 16;
 
-    private Boolean autoRead = true;
+    private Boolean autoRead = false;
 
     /**
      * options: unpooled or pooled.
@@ -49,6 +53,16 @@ public class NettyChannelProperties {
 
     private Integer soRcvBuf = 87380;
 
+    /**
+     * message estimator.
+     */
+    private Integer messageSizeEstimator = 8;
+
+    /**
+     * single event executor.
+     */
+    private Boolean singleEventExecutorPerGroup = true;
+
     /**
      * get connectTimeoutMillis.
      *
@@ -201,4 +215,50 @@ public class NettyChannelProperties {
     public void setSoRcvBuf(final Integer soRcvBuf) {
         this.soRcvBuf = soRcvBuf;
     }
+
+    /**
+     * message size estimator.
+     *
+     * @return MessageSizeEstimator
+     */
+    public MessageSizeEstimator getMessageSizeEstimatorBuild() {
+        return Objects.isNull(messageSizeEstimator) || messageSizeEstimator == 8 ? DefaultMessageSizeEstimator.DEFAULT
+                : new DefaultMessageSizeEstimator(this.getMessageSizeEstimator());
+    }
+
+    /**
+     * get message size estimator.
+     *
+     * @return message size estimator
+     */
+    public Integer getMessageSizeEstimator() {
+        return messageSizeEstimator;
+    }
+
+    /**
+     * set message size estimator.
+     *
+     * @param messageSizeEstimator messageSizeEstimator
+     */
+    public void setMessageSizeEstimator(final Integer messageSizeEstimator) {
+        this.messageSizeEstimator = messageSizeEstimator;
+    }
+
+    /**
+     * single event executor.
+     *
+     * @return single event executor state
+     */
+    public Boolean getSingleEventExecutorPerGroup() {
+        return singleEventExecutorPerGroup;
+    }
+
+    /**
+     * set single event executor.
+     *
+     * @param singleEventExecutorPerGroup single executor
+     */
+    public void setSingleEventExecutorPerGroup(final Boolean singleEventExecutorPerGroup) {
+        this.singleEventExecutorPerGroup = singleEventExecutorPerGroup;
+    }
 }
diff --git a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-gateway/src/main/java/org/apache/shenyu/springboot/starter/netty/NettyTcpProperties.java b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-gateway/src/main/java/org/apache/shenyu/springboot/starter/netty/NettyHttpProperties.java
similarity index 93%
rename from shenyu-spring-boot-starter/shenyu-spring-boot-starter-gateway/src/main/java/org/apache/shenyu/springboot/starter/netty/NettyTcpProperties.java
rename to shenyu-spring-boot-starter/shenyu-spring-boot-starter-gateway/src/main/java/org/apache/shenyu/springboot/starter/netty/NettyHttpProperties.java
index d6f95078e..b0cb71eac 100644
--- a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-gateway/src/main/java/org/apache/shenyu/springboot/starter/netty/NettyTcpProperties.java
+++ b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-gateway/src/main/java/org/apache/shenyu/springboot/starter/netty/NettyHttpProperties.java
@@ -20,7 +20,7 @@ package org.apache.shenyu.springboot.starter.netty;
 /**
  * The netty tcp properties.
  */
-public class NettyTcpProperties {
+public class NettyHttpProperties {
 
     private Boolean webServerFactoryEnabled = true;
 
@@ -28,6 +28,8 @@ public class NettyTcpProperties {
 
     private Integer workerCount = Runtime.getRuntime().availableProcessors() << 1;
 
+    private Boolean accessLog = false;
+
     private ServerSocketChannelProperties serverSocketChannel = new ServerSocketChannelProperties();
 
     private SocketChannelProperties socketChannel = new SocketChannelProperties();
@@ -123,6 +125,24 @@ public class NettyTcpProperties {
         return socketChannel;
     }
 
+    /**
+     * get access log state.
+     *
+     * @return access log state
+     */
+    public Boolean getAccessLog() {
+        return accessLog;
+    }
+
+    /**
+     * set access log state.
+     *
+     * @param accessLog access log state
+     */
+    public void setAccessLog(final Boolean accessLog) {
+        this.accessLog = accessLog;
+    }
+
     public static class ServerSocketChannelProperties extends NettyChannelProperties {
 
         private Integer soBacklog = 128;
diff --git a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-gateway/src/main/java/org/apache/shenyu/springboot/starter/netty/ShenyuNettyWebServerConfiguration.java b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-gateway/src/main/java/org/apache/shenyu/springboot/starter/netty/ShenyuNettyWebServerConfiguration.java
index 2d81cdacf..f49fbbe39 100644
--- a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-gateway/src/main/java/org/apache/shenyu/springboot/starter/netty/ShenyuNettyWebServerConfiguration.java
+++ b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-gateway/src/main/java/org/apache/shenyu/springboot/starter/netty/ShenyuNettyWebServerConfiguration.java
@@ -43,9 +43,9 @@ public class ShenyuNettyWebServerConfiguration {
      * @return the netty tcp config
      */
     @Bean
-    @ConfigurationProperties(prefix = "shenyu.netty.tcp")
-    public NettyTcpProperties nettyTcpProperties() {
-        return new NettyTcpProperties();
+    @ConfigurationProperties(prefix = "shenyu.netty.http")
+    public NettyHttpProperties nettyTcpProperties() {
+        return new NettyHttpProperties();
     }
     
     /**
@@ -55,40 +55,63 @@ public class ShenyuNettyWebServerConfiguration {
      * @return the netty reactive web server factory
      */
     @Bean
-    @ConditionalOnProperty(value = "shenyu.netty.tcp.webServerFactoryEnabled", havingValue = "true", matchIfMissing = true)
-    public NettyReactiveWebServerFactory nettyReactiveWebServerFactory(final ObjectProvider<NettyTcpProperties> properties) {
+    @ConditionalOnProperty(value = "shenyu.netty.http.webServerFactoryEnabled", havingValue = "true", matchIfMissing = true)
+    public NettyReactiveWebServerFactory nettyReactiveWebServerFactory(final ObjectProvider<NettyHttpProperties> properties) {
         NettyReactiveWebServerFactory webServerFactory = new NettyReactiveWebServerFactory();
-        NettyTcpProperties nettyTcpProperties = Optional.ofNullable(properties.getIfAvailable()).orElse(new NettyTcpProperties());
-        webServerFactory.addServerCustomizers(new EventLoopNettyCustomizer(nettyTcpProperties));
+        NettyHttpProperties nettyHttpProperties = Optional.ofNullable(properties.getIfAvailable()).orElse(new NettyHttpProperties());
+        webServerFactory.addServerCustomizers(new EventLoopNettyCustomizer(nettyHttpProperties));
         return webServerFactory;
     }
 
     private static class EventLoopNettyCustomizer implements NettyServerCustomizer {
 
-        private final NettyTcpProperties nettyTcpProperties;
+        private final NettyHttpProperties nettyHttpProperties;
     
         /**
          * Instantiates a new Event loop netty customizer.
          *
-         * @param nettyTcpProperties the netty tcp config
+         * @param nettyHttpProperties the netty tcp config
          */
-        EventLoopNettyCustomizer(final NettyTcpProperties nettyTcpProperties) {
-            this.nettyTcpProperties = nettyTcpProperties;
+        EventLoopNettyCustomizer(final NettyHttpProperties nettyHttpProperties) {
+            this.nettyHttpProperties = nettyHttpProperties;
         }
 
         @Override
         public HttpServer apply(final HttpServer httpServer) {
-            // TODO moremind: optimize httpServer
-            return httpServer.runOn(LoopResources.create("shenyu-netty", nettyTcpProperties.getSelectCount(), nettyTcpProperties.getWorkerCount(), true))
-                    .option(ChannelOption.SO_BACKLOG, nettyTcpProperties.getServerSocketChannel().getSoBacklog())
-                    .option(ChannelOption.SO_REUSEADDR, nettyTcpProperties.getServerSocketChannel().isSoReuseAddr())
-                    .option(ChannelOption.SO_RCVBUF, nettyTcpProperties.getServerSocketChannel().getSoRcvBuf())
-                    .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, nettyTcpProperties.getServerSocketChannel().getConnectTimeoutMillis())
-                    .option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(nettyTcpProperties.getServerSocketChannel().getWriteBufferLowWaterMark(),
-                            nettyTcpProperties.getServerSocketChannel().getWriteBufferHighWaterMark()))
-                    .option(ChannelOption.WRITE_SPIN_COUNT, nettyTcpProperties.getServerSocketChannel().getWriteSpinCount())
-                    .childOption(ChannelOption.AUTO_READ, false)
-                    .option(ChannelOption.ALLOCATOR, nettyTcpProperties.getServerSocketChannel().getAllocator());
+            return httpServer.runOn(LoopResources.create("shenyu-netty", nettyHttpProperties.getSelectCount(), nettyHttpProperties.getWorkerCount(), true))
+                    .accessLog(nettyHttpProperties.getAccessLog())
+                    // server socket channel parameters
+                    .option(ChannelOption.SO_BACKLOG, nettyHttpProperties.getServerSocketChannel().getSoBacklog())
+                    .option(ChannelOption.SO_REUSEADDR, nettyHttpProperties.getServerSocketChannel().isSoReuseAddr())
+                    .option(ChannelOption.SO_RCVBUF, nettyHttpProperties.getServerSocketChannel().getSoRcvBuf())
+                    // common parameters
+                    .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, nettyHttpProperties.getServerSocketChannel().getConnectTimeoutMillis())
+                    .option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(nettyHttpProperties.getServerSocketChannel().getWriteBufferLowWaterMark(),
+                            nettyHttpProperties.getServerSocketChannel().getWriteBufferHighWaterMark()))
+                    .option(ChannelOption.WRITE_SPIN_COUNT, nettyHttpProperties.getServerSocketChannel().getWriteSpinCount())
+                    .option(ChannelOption.AUTO_READ, nettyHttpProperties.getServerSocketChannel().isAutoRead())
+                    .option(ChannelOption.ALLOCATOR, nettyHttpProperties.getServerSocketChannel().getAllocator())
+                    .option(ChannelOption.MESSAGE_SIZE_ESTIMATOR, nettyHttpProperties.getServerSocketChannel().getMessageSizeEstimatorBuild())
+                    .option(ChannelOption.SINGLE_EVENTEXECUTOR_PER_GROUP, nettyHttpProperties.getServerSocketChannel().getSingleEventExecutorPerGroup())
+
+                    // socket channel parameters
+                    .childOption(ChannelOption.SO_KEEPALIVE, nettyHttpProperties.getSocketChannel().isSoKeepAlive())
+                    .childOption(ChannelOption.SO_LINGER, nettyHttpProperties.getSocketChannel().getSoLinger())
+                    .childOption(ChannelOption.TCP_NODELAY, nettyHttpProperties.getSocketChannel().isTcpNoDelay())
+                    .childOption(ChannelOption.SO_SNDBUF, nettyHttpProperties.getSocketChannel().getSoSndBuf())
+                    .childOption(ChannelOption.IP_TOS, nettyHttpProperties.getSocketChannel().getIpTos())
+                    .childOption(ChannelOption.ALLOW_HALF_CLOSURE, nettyHttpProperties.getSocketChannel().isAllowHalfClosure())
+                    .childOption(ChannelOption.SO_RCVBUF, nettyHttpProperties.getSocketChannel().getSoRcvBuf())
+                    .childOption(ChannelOption.SO_REUSEADDR, nettyHttpProperties.getSocketChannel().isSoReuseAddr())
+                    // common parameters
+                    .childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, nettyHttpProperties.getSocketChannel().getConnectTimeoutMillis())
+                    .childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(nettyHttpProperties.getSocketChannel().getWriteBufferLowWaterMark(),
+                    nettyHttpProperties.getSocketChannel().getWriteBufferHighWaterMark()))
+                    .childOption(ChannelOption.WRITE_SPIN_COUNT, nettyHttpProperties.getSocketChannel().getWriteSpinCount())
+                    .childOption(ChannelOption.AUTO_READ, nettyHttpProperties.getSocketChannel().isAutoRead())
+                    .childOption(ChannelOption.ALLOCATOR, nettyHttpProperties.getSocketChannel().getAllocator())
+                    .childOption(ChannelOption.MESSAGE_SIZE_ESTIMATOR, nettyHttpProperties.getSocketChannel().getMessageSizeEstimatorBuild())
+                    .childOption(ChannelOption.SINGLE_EVENTEXECUTOR_PER_GROUP, nettyHttpProperties.getSocketChannel().getSingleEventExecutorPerGroup());
         }
     }
 }
diff --git a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-gateway/src/test/java/org/apache/shenyu/springboot/starter/netty/ShenyuNettyWebServerConfigurationTest.java b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-gateway/src/test/java/org/apache/shenyu/springboot/starter/netty/ShenyuNettyWebServerConfigurationTest.java
index 0d4172746..66ad9a94f 100644
--- a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-gateway/src/test/java/org/apache/shenyu/springboot/starter/netty/ShenyuNettyWebServerConfigurationTest.java
+++ b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-gateway/src/test/java/org/apache/shenyu/springboot/starter/netty/ShenyuNettyWebServerConfigurationTest.java
@@ -49,29 +49,33 @@ public class ShenyuNettyWebServerConfigurationTest {
         applicationContextRunner
             .withPropertyValues(
                 "debug=true",
-                "shenyu.netty.tcp.webServerFactoryEnabled=false",
-                "shenyu.netty.tcp.selectCount=10",
-                "shenyu.netty.tcp.workerCount=2",
-                "shenyu.netty.tcp.serverSocketChannel.soBacklog=64",
-                "shenyu.netty.tcp.serverSocketChannel.connectTimeoutMillis=65536",
-                "shenyu.netty.tcp.serverSocketChannel.writeBufferLowWaterMark=65536",
-                "shenyu.netty.tcp.serverSocketChannel.soRcvBuf=65536",
-                "shenyu.netty.tcp.socketChannel.soSndBuf=128",
-                "shenyu.netty.tcp.socketChannel.ipTos=64",
-                "shenyu.netty.tcp.socketChannel.soKeepAlive=true",
-                "shenyu.netty.tcp.socketChannel.allowHalfClosure=true",
-                "shenyu.netty.tcp.socketChannel.soLinger=10",
-                "shenyu.netty.tcp.socketChannel.tcpNoDelay=false",
-                "shenyu.netty.tcp.socketChannel.writeBufferHighWaterMark=256",
-                "shenyu.netty.tcp.socketChannel.writeBufferLowWaterMark=128",
-                "shenyu.netty.tcp.socketChannel.writeSpinCount=8",
-                "shenyu.netty.tcp.socketChannel.autoRead=false",
-                "shenyu.netty.tcp.socketChannel.soReuseAddr=true",
-                "shenyu.netty.tcp.socketChannel.soRcvBuf=65536"
+                "shenyu.netty.http.accessLog=false",
+                "shenyu.netty.http.webServerFactoryEnabled=false",
+                "shenyu.netty.http.selectCount=10",
+                "shenyu.netty.http.workerCount=2",
+                "shenyu.netty.http.serverSocketChannel.soBacklog=64",
+                "shenyu.netty.http.serverSocketChannel.connectTimeoutMillis=65536",
+                "shenyu.netty.http.serverSocketChannel.writeBufferLowWaterMark=65536",
+                "shenyu.netty.http.serverSocketChannel.soRcvBuf=65536",
+                "shenyu.netty.http.socketChannel.soSndBuf=128",
+                "shenyu.netty.http.socketChannel.ipTos=64",
+                "shenyu.netty.http.socketChannel.soKeepAlive=true",
+                "shenyu.netty.http.socketChannel.allowHalfClosure=true",
+                "shenyu.netty.http.socketChannel.soLinger=10",
+                "shenyu.netty.http.socketChannel.tcpNoDelay=false",
+                "shenyu.netty.http.socketChannel.writeBufferHighWaterMark=256",
+                "shenyu.netty.http.socketChannel.writeBufferLowWaterMark=128",
+                "shenyu.netty.http.socketChannel.writeSpinCount=8",
+                "shenyu.netty.http.socketChannel.autoRead=false",
+                "shenyu.netty.http.socketChannel.soReuseAddr=true",
+                "shenyu.netty.http.socketChannel.soRcvBuf=65536",
+                "shenyu.netty.http.socketChannel.messageSizeEstimator=8",
+                "shenyu.netty.http.socketChannel.singleEventExecutorPerGroup=false"
             )
             .run(context -> {
-                NettyTcpProperties properties = context.getBean("nettyTcpProperties", NettyTcpProperties.class);
+                NettyHttpProperties properties = context.getBean("nettyTcpProperties", NettyHttpProperties.class);
                 assertNotNull(properties);
+                assertThat(properties.getAccessLog(), is(false));
                 assertThat(properties.isWebServerFactoryEnabled(), is(false));
                 assertThat(properties.getSelectCount(), is(10));
                 assertThat(properties.getWorkerCount(), is(2));
@@ -93,6 +97,8 @@ public class ShenyuNettyWebServerConfigurationTest {
                 assertThat(properties.getSocketChannel().isAutoRead(), is(false));
                 assertThat(properties.getSocketChannel().isSoReuseAddr(), is(true));
                 assertThat(properties.getSocketChannel().getSoRcvBuf(), is(65536));
+                assertThat(properties.getSocketChannel().getMessageSizeEstimator(), is(8));
+                assertThat(properties.getSocketChannel().getSingleEventExecutorPerGroup(), is(false));
             });
     }
 }