[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强制关闭核心抖音场景必须 // 第二步获取底层Socket强制关闭核心抖音场景必须
if (channel instanceof NioSocketChannel) { if (channel instanceof NioSocketChannel) {
NioSocketChannel nioChannel = (NioSocketChannel) channel; NioSocketChannel nioChannel = (NioSocketChannel) channel;
Socket socket = nioChannel.javaChannel().socket();
if (socket != null && !socket.isClosed()) { // Socket socket = nioChannel.javaChannel().socket();
socket.setSoLinger(true, 0); // 禁用SO_LINGER立即关闭 // if (socket != null && !socket.isClosed()) {
socket.close(); // 强制关闭Socket切断TCP连接 // socket.setSoLinger(true, 0); // 禁用SO_LINGER立即关闭
} // socket.close(); // 强制关闭Socket切断TCP连接
// }
} }
// 第三步关闭Netty通道兜底 // 第三步关闭Netty通道兜底

View File

@@ -48,8 +48,8 @@ public class NettySocketProtocolHandleSocketLocalProxyTypeAdvanced
b.group(group) b.group(group)
.channel(NioSocketChannel.class) .channel(NioSocketChannel.class)
.option(ChannelOption.SO_KEEPALIVE, true) // 开启TCP保活检测无效连接 // .option(ChannelOption.SO_KEEPALIVE, true) // 开启TCP保活检测无效连接
.option(ChannelOption.TCP_NODELAY, true) // 关闭Nagle算法适配直播流低延迟 // .option(ChannelOption.TCP_NODELAY, true) // 关闭Nagle算法适配直播流低延迟
.option(ChannelOption.SO_RCVBUF, 2048 * 1024)// 设置读缓冲区为2M .option(ChannelOption.SO_RCVBUF, 2048 * 1024)// 设置读缓冲区为2M
.option(ChannelOption.SO_SNDBUF, 1024 * 1024)// 设置写缓冲区为1M .option(ChannelOption.SO_SNDBUF, 1024 * 1024)// 设置写缓冲区为1M
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000 * 60)//连接超时时间设置为 60 秒 .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.ByteBuf;
import io.netty.buffer.Unpooled; 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 io.netty.util.ReferenceCountUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.common.NettyByteBuf; 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.Message;
import org.framework.wu.framework.queue.MessageQueue; import org.framework.wu.framework.queue.MessageQueue;
import org.framework.wu.framework.queue.MessageQueueFactory; import org.framework.wu.framework.queue.MessageQueueFactory;
import org.wu.framework.core.utils.ObjectUtils;
import org.wu.framework.spring.utils.SpringContextHolder; import org.wu.framework.spring.utils.SpringContextHolder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@@ -23,6 +24,7 @@ import java.nio.charset.StandardCharsets;
public class NettySocketBackendHandler extends SimpleChannelInboundHandler<NettyByteBuf> { public class NettySocketBackendHandler extends SimpleChannelInboundHandler<NettyByteBuf> {
@Override @Override
public void channelActive(ChannelHandlerContext ctx) { public void channelActive(ChannelHandlerContext ctx) {
ctx.writeAndFlush(Unpooled.EMPTY_BUFFER); ctx.writeAndFlush(Unpooled.EMPTY_BUFFER);
@@ -72,6 +74,8 @@ public class NettySocketBackendHandler extends SimpleChannelInboundHandler<Netty
nextChannel.writeAndFlush(buf); nextChannel.writeAndFlush(buf);
} else { } else {
log.info("释放内存"); log.info("释放内存");
// 4.2.9 暴力彻底关闭,杜绝半关闭
ctx.channel().unsafe().closeForcibly();
ReferenceCountUtil.release(buf); ReferenceCountUtil.release(buf);
} }
} }
@@ -89,6 +93,8 @@ public class NettySocketBackendHandler extends SimpleChannelInboundHandler<Netty
@Override @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
log.error("NettySocketBackendHandler exception", cause); 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 { public void channelInactive(ChannelHandlerContext ctx) throws Exception {
log.trace("代理通道与客户通道断开,即将断开客户端和代理服务器的连接"); log.trace("代理通道与客户通道断开,即将断开客户端和代理服务器的连接");
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(ctx.channel()); Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(ctx.channel());
if (nextChannel.isActive()) { if (nextChannel != null && nextChannel.isActive()) {
nextChannel.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); nextChannel.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
} }
super.channelInactive(ctx); super.channelInactive(ctx);