Skip to content

Commit a4060c6

Browse files
author
wangzihao
committed
发布2.3.23
hotfix: bug# 解决客户端的tcp连接建立繁忙时,客户端会出现connection reset
1 parent 2190b51 commit a4060c6

6 files changed

Lines changed: 89 additions & 24 deletions

File tree

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ github地址 : https://github.com/wangzihaogithub/spring-boot-protocol
149149
<dependency>
150150
<groupId>com.github.wangzihaogithub</groupId>
151151
<artifactId>spring-boot-protocol</artifactId>
152-
<version>2.3.22</version>
152+
<version>2.3.23</version>
153153
</dependency>
154154
```
155155

@@ -161,7 +161,7 @@ github地址 : https://github.com/wangzihaogithub/spring-boot-protocol
161161
<dependency>
162162
<groupId>com.github.wangzihaogithub</groupId>
163163
<artifactId>netty-servlet</artifactId>
164-
<version>2.3.22</version>
164+
<version>2.3.23</version>
165165
</dependency>
166166
```
167167

@@ -317,7 +317,7 @@ github地址 : https://github.com/wangzihaogithub/spring-boot-protocol
317317
<dependency>
318318
<groupId>com.github.wangzihaogithub</groupId>
319319
<artifactId>spring-boot-protocol</artifactId>
320-
<version>2.3.22</version>
320+
<version>2.3.23</version>
321321
</dependency>
322322

323323
2.编写代码
@@ -397,7 +397,7 @@ github地址 : https://github.com/wangzihaogithub/spring-boot-protocol
397397
<dependency>
398398
<groupId>com.github.wangzihaogithub</groupId>
399399
<artifactId>spring-boot-protocol</artifactId>
400-
<version>2.3.22</version>
400+
<version>2.3.23</version>
401401
</dependency>
402402

403403
2.编写代码
@@ -486,7 +486,7 @@ github地址 : https://github.com/wangzihaogithub/spring-boot-protocol
486486
<dependency>
487487
<groupId>com.github.wangzihaogithub</groupId>
488488
<artifactId>spring-boot-protocol</artifactId>
489-
<version>2.3.22</version>
489+
<version>2.3.23</version>
490490
</dependency>
491491
492492
2.编写启动类

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.github.wangzihaogithub</groupId>
66
<artifactId>spring-boot-protocol</artifactId>
7-
<version>2.3.22</version>
7+
<version>2.3.23</version>
88
<packaging>jar</packaging>
99

1010
<name>Spring Boot Protocol</name>
@@ -49,7 +49,7 @@
4949
<connection>scm:git:https://github.com/wangzihaogithub/spring-boot-protocol.git</connection>
5050
<developerConnection>scm:git:git@github.com:wangzihaogithub/spring-boot-protocol.git</developerConnection>
5151
<url>git@github.com:wangzihaogithub/spring-boot-protocol.git</url>
52-
<tag>v2.3.22</tag>
52+
<tag>v2.3.23</tag>
5353
</scm>
5454

5555
<!-- 开发者信息 -->

src/main/java/com/github/netty/protocol/DynamicProtocolChannelHandler.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class DynamicProtocolChannelHandler extends AbstractChannelHandler<ByteBu
4848
* When there is a new link Access, if the packet is confiscated in time,
4949
* the server will turn off the link or perform timeout processing.
5050
*/
51-
private long firstClientPacketReadTimeoutMs = 1000;
51+
private long firstClientPacketReadTimeoutMs = 800;
5252

5353
public DynamicProtocolChannelHandler() {
5454
super(false);
@@ -63,11 +63,13 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception {
6363
TcpChannel tcpChannel = new TcpChannel(channel, this);
6464
addConnection(id, tcpChannel);
6565

66-
ctx.executor().schedule(() -> {
67-
if (tcpChannel.getProtocol() == null && tcpChannel.isActive()) {
68-
onProtocolBindTimeout(ctx, tcpChannel);
69-
}
70-
}, firstClientPacketReadTimeoutMs, TimeUnit.MILLISECONDS);
66+
if (protocolHandlers.size() > 1 && firstClientPacketReadTimeoutMs >= 0) {
67+
ctx.executor().schedule(() -> {
68+
if (tcpChannel.getProtocol() == null && tcpChannel.isActive()) {
69+
onProtocolBindTimeout(ctx, tcpChannel);
70+
}
71+
}, firstClientPacketReadTimeoutMs, TimeUnit.MILLISECONDS);
72+
}
7173

7274
channel.pipeline().addLast(removeTcpChannelHandler);
7375
if (bytesMetricsChannelHandler != null) {
@@ -127,6 +129,9 @@ protected void addPipeline(ChannelHandlerContext ctx, ProtocolHandler protocolHa
127129
}
128130

129131
public ProtocolHandler getProtocolHandler(ByteBuf clientFirstMsg) {
132+
if (protocolHandlers.size() == 1) {
133+
return protocolHandlers.iterator().next();
134+
}
130135
for (ProtocolHandler protocolHandler : protocolHandlers) {
131136
if (protocolHandler.canSupport(clientFirstMsg)) {
132137
return protocolHandler;
@@ -136,6 +141,9 @@ public ProtocolHandler getProtocolHandler(ByteBuf clientFirstMsg) {
136141
}
137142

138143
public ProtocolHandler getProtocolHandler(Channel channel) {
144+
if (protocolHandlers.size() == 1) {
145+
return protocolHandlers.iterator().next();
146+
}
139147
for (ProtocolHandler protocolHandler : protocolHandlers) {
140148
if (protocolHandler.canSupport(channel)) {
141149
return protocolHandler;
@@ -184,7 +192,9 @@ protected void onProtocolBindTimeout(ChannelHandlerContext ctx, TcpChannel tcpCh
184192

185193
protected void onNoSupportProtocol(ChannelHandlerContext ctx, ByteBuf clientFirstMsg) {
186194
if (clientFirstMsg != null) {
187-
logger.warn("Received no support protocol. message=[{}]", clientFirstMsg.toString(Charset.forName("UTF-8")));
195+
if (logger.isWarnEnabled()) {
196+
logger.warn("Received no support protocol. message=[{}]", clientFirstMsg.toString(Charset.forName("UTF-8")));
197+
}
188198
if (clientFirstMsg.refCnt() > 0) {
189199
clientFirstMsg.release();
190200
}
@@ -214,7 +224,9 @@ public int getConnectionCount() {
214224

215225
@Override
216226
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
217-
logger.warn("Failed to initialize a channel. Closing: " + ctx.channel(), cause);
227+
if (logger.isWarnEnabled()) {
228+
logger.warn("Failed to initialize a channel. Closing: " + ctx.channel(), cause);
229+
}
218230
ctx.close();
219231
}
220232

@@ -248,8 +260,11 @@ public void enableTcpPackageLog(LogLevel logLevel) {
248260
public class RemoveTcpChannelHandler extends ChannelInboundHandlerAdapter {
249261
@Override
250262
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
251-
super.channelInactive(ctx);
252-
removeConnection(ctx.channel().id());
263+
try {
264+
super.channelInactive(ctx);
265+
} finally {
266+
removeConnection(ctx.channel().id());
267+
}
253268
}
254269
}
255270

src/main/java/com/github/netty/springboot/NettyProperties.java

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,26 @@ public class NettyProperties implements Serializable {
9191
* 是否禁用Nagle算法,true=禁用Nagle算法. 即数据包立即发送出去 (在TCP_NODELAY模式下,假设有3个小包要发送,第一个小包发出后,接下来的小包需要等待之前的小包被ack,在这期间小包会合并,直到接收到之前包的ack后才会发生)
9292
*/
9393
private boolean tcpNodelay = false;
94+
95+
/**
96+
* 用于设置写缓冲区的低水位线和高水位线。
97+
* 如果写入缓冲区中排队的字节数超过高水位线,Channel#isWritable将开始返回false
98+
* 如果写入缓冲区中排队的字节数超过高水位线然后下降到低水位线以下,Channel#isWritable将开始返回true
99+
* 一般影响http2,请注意,消息需要由@link MessageSizeEstimator处理,以提供精确的背压。
100+
*/
101+
private int lowWaterMark = 32 * 1024;
102+
103+
/**
104+
* 用于设置写缓冲区的低水位线和高水位线。
105+
* 如果写入缓冲区中排队的字节数超过高水位线,Channel#isWritable将开始返回false
106+
* 如果写入缓冲区中排队的字节数超过高水位线然后下降到低水位线以下,Channel#isWritable将开始返回true
107+
* 一般影响http2,请注意,消息需要由@link MessageSizeEstimator处理,以提供精确的背压。
108+
*/
109+
private int highWaterMark = Integer.MAX_VALUE;
110+
/**
111+
* 如果true,则Netty会在Channel写入失败时会自动立即关闭。默认值为true
112+
*/
113+
private boolean autoClose = true;
94114
/**
95115
* tcp 接收数据缓冲区大小字节 (默认-1 跟随内核设置)
96116
*/
@@ -100,9 +120,15 @@ public class NettyProperties implements Serializable {
100120
*/
101121
private int soSndbuf = -1;
102122
/**
103-
* tcp 服务端接收新连接,等待被accept出的, 队列大小 (默认50)
123+
* tcp 服务端接收新连接,等待被accept出的, 队列大小 (默认-1 跟随内核设置)
124+
* 确定平台的默认somaxconn(服务器套接字积压)值。
125+
* 已知默认值:-Windows NT服务器4.0+:200,Linux和MacOS:128
126+
* 查看命令
127+
* cat /proc/sys/net/core/somaxconn
128+
* sysctl kern.ipc.somaxconn
129+
* sysctl kern.ipc.soacceptqueue
104130
*/
105-
private int soBacklog = 50;
131+
private int soBacklog = -1;
106132
/**
107133
* netty的内存泄漏检测级别(调试程序的时候用). 默认禁用, 不然极其耗费性能
108134
*/
@@ -115,6 +141,22 @@ public class NettyProperties implements Serializable {
115141
public NettyProperties() {
116142
}
117143

144+
public int getLowWaterMark() {
145+
return lowWaterMark;
146+
}
147+
148+
public void setLowWaterMark(int lowWaterMark) {
149+
this.lowWaterMark = lowWaterMark;
150+
}
151+
152+
public int getHighWaterMark() {
153+
return highWaterMark;
154+
}
155+
156+
public void setHighWaterMark(int highWaterMark) {
157+
this.highWaterMark = highWaterMark;
158+
}
159+
118160
public ApplicationX getApplication() {
119161
return application;
120162
}
@@ -127,6 +169,14 @@ public void setSoBacklog(int soBacklog) {
127169
this.soBacklog = soBacklog;
128170
}
129171

172+
public boolean isAutoClose() {
173+
return autoClose;
174+
}
175+
176+
public void setAutoClose(boolean autoClose) {
177+
this.autoClose = autoClose;
178+
}
179+
130180
public int getSoRcvbuf() {
131181
return soRcvbuf;
132182
}

src/main/java/com/github/netty/springboot/server/NettyTcpServer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ protected void config(ServerBootstrap bootstrap) throws Exception {
120120
System.setProperty("io.netty.maxDirectMemory", String.valueOf(maxDirectMemory));
121121
}
122122
// bootstrap.childOption(ChannelOption.WRITE_SPIN_COUNT,Integer.MAX_VALUE);
123-
bootstrap.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(32 * 1024, Integer.MAX_VALUE));
124-
bootstrap.childOption(ChannelOption.AUTO_CLOSE, true);
123+
bootstrap.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(properties.getLowWaterMark(), properties.getHighWaterMark()));
124+
bootstrap.childOption(ChannelOption.AUTO_CLOSE, properties.isAutoClose());
125125
if (properties.getSoRcvbuf() > 0) {
126126
bootstrap.childOption(ChannelOption.SO_RCVBUF, properties.getSoRcvbuf());
127127
}

src/main/resources/server.properties

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16-
server.info=Github Spring-boot-protocol/2.3.22
17-
server.number=2.3.22
18-
server.built=Aug 6 2024 19:09:02 UTC+8
16+
server.info=Github Spring-boot-protocol/2.3.23
17+
server.number=2.3.23
18+
server.built=Sep 13 2024 13:23:31 UTC+8

0 commit comments

Comments
 (0)