[update] 代理模式无法关闭第三方消息问题

This commit is contained in:
wujiawei
2026-01-11 17:35:31 +08:00
parent 0c6fb1680a
commit d797b918a4
4 changed files with 19 additions and 12 deletions

View File

@@ -94,11 +94,12 @@ public class ChannelUtils {
// 第二步获取底层Socket强制关闭核心抖音场景必须
if (channel instanceof NioSocketChannel) {
NioSocketChannel nioChannel = (NioSocketChannel) channel;
Socket socket = nioChannel.javaChannel().socket();
if (socket != null && !socket.isClosed()) {
socket.setSoLinger(true, 0); // 禁用SO_LINGER立即关闭
socket.close(); // 强制关闭Socket切断TCP连接
}
// Socket socket = nioChannel.javaChannel().socket();
// if (socket != null && !socket.isClosed()) {
// socket.setSoLinger(true, 0); // 禁用SO_LINGER立即关闭
// socket.close(); // 强制关闭Socket切断TCP连接
// }
}
// 第三步关闭Netty通道兜底

View File

@@ -48,8 +48,8 @@ public class NettySocketProtocolHandleSocketLocalProxyTypeAdvanced
b.group(group)
.channel(NioSocketChannel.class)
.option(ChannelOption.SO_KEEPALIVE, true) // 开启TCP保活检测无效连接
.option(ChannelOption.TCP_NODELAY, true) // 关闭Nagle算法适配直播流低延迟
// .option(ChannelOption.SO_KEEPALIVE, true) // 开启TCP保活检测无效连接
// .option(ChannelOption.TCP_NODELAY, true) // 关闭Nagle算法适配直播流低延迟
.option(ChannelOption.SO_RCVBUF, 2048 * 1024)// 设置读缓冲区为2M
.option(ChannelOption.SO_SNDBUF, 1024 * 1024)// 设置写缓冲区为1M
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000 * 60)//连接超时时间设置为 60 秒

View File

@@ -2,7 +2,10 @@ package org.framework.lazy.cloud.network.heartbeat.protocol.handler;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.ReferenceCountUtil;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.common.NettyByteBuf;
@@ -12,10 +15,8 @@ import org.framework.lazy.cloud.network.heartbeat.protocol.properties.ProtocolPr
import org.framework.wu.framework.queue.Message;
import org.framework.wu.framework.queue.MessageQueue;
import org.framework.wu.framework.queue.MessageQueueFactory;
import org.wu.framework.core.utils.ObjectUtils;
import org.wu.framework.spring.utils.SpringContextHolder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
@@ -23,6 +24,7 @@ import java.nio.charset.StandardCharsets;
public class NettySocketBackendHandler extends SimpleChannelInboundHandler<NettyByteBuf> {
@Override
public void channelActive(ChannelHandlerContext ctx) {
ctx.writeAndFlush(Unpooled.EMPTY_BUFFER);
@@ -72,6 +74,8 @@ public class NettySocketBackendHandler extends SimpleChannelInboundHandler<Netty
nextChannel.writeAndFlush(buf);
} else {
log.info("释放内存");
// 4.2.9 暴力彻底关闭,杜绝半关闭
ctx.channel().unsafe().closeForcibly();
ReferenceCountUtil.release(buf);
}
}
@@ -89,6 +93,8 @@ public class NettySocketBackendHandler extends SimpleChannelInboundHandler<Netty
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
log.error("NettySocketBackendHandler exception", cause);
ctx.close();
// ctx.close();
// 4.2.9 暴力彻底关闭,杜绝半关闭
ctx.channel().unsafe().closeForcibly();
}
}

View File

@@ -113,7 +113,7 @@ public class NettySocks5CommandRequestHandler extends SimpleChannelInboundHandle
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
log.trace("代理通道与客户通道断开,即将断开客户端和代理服务器的连接");
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(ctx.channel());
if (nextChannel.isActive()) {
if (nextChannel != null && nextChannel.isActive()) {
nextChannel.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}
super.channelInactive(ctx);