代理优化,解决背压下载大文件中途断开的问题

This commit is contained in:
aoshiguchen
2023-09-23 23:21:13 +08:00
parent 840dd2d344
commit 268e17273e
10 changed files with 92 additions and 21 deletions
@@ -49,6 +49,17 @@ public class RealServerChannelHandler extends SimpleChannelInboundHandler<ByteBu
// 代理客户端连接断开
ctx.channel().close();
} else {
if (proxyChannel.isWritable()) {
if (!realServerChannel.config().isAutoRead()) {
realServerChannel.config().setAutoRead(true);
}
} else {
if (realServerChannel.config().isAutoRead()) {
realServerChannel.config().setAutoRead(false);
}
}
byte[] bytes = new byte[buf.readableBytes()];
buf.readBytes(bytes);
String visitorId = ProxyUtil.getVisitorIdByRealServerChannel(realServerChannel);
@@ -63,16 +63,18 @@ public class TcpProxyChannelHandler extends SimpleChannelInboundHandler<ProxyMes
IdleStateEvent event = (IdleStateEvent)evt;
switch (event.state()) {
case READER_IDLE:
// 读超时,断开连接
log.info("[TCP Proxy Channel]Read timeout");
ctx.channel().close();
if (ctx.channel().isWritable()) {
// 读超时,断开连接
log.info("[TCP Proxy Channel]Read timeout");
ctx.channel().close();
}
break;
case WRITER_IDLE:
ctx.channel().writeAndFlush(ProxyMessage.buildHeartbeatMessage());
break;
case ALL_IDLE:
log.debug("[TCP Proxy Channel]ReadWrite timeout");
ctx.close();
// log.debug("[TCP Proxy Channel]ReadWrite timeout");
// ctx.close();
break;
}
}
@@ -23,6 +23,10 @@ public class ProxyMessageTransferHandler implements ProxyMessageHandler {
public void handle(ChannelHandlerContext ctx, ProxyMessage proxyMessage) {
Channel realServerChannel = ctx.channel().attr(Constants.NEXT_CHANNEL).get();
if (realServerChannel != null) {
// 自己可写,则设置来源可读。自己不可写,则设置来源不可读
realServerChannel.config().setAutoRead(ctx.channel().isWritable());
ByteBuf buf = ctx.alloc().buffer(proxyMessage.getData().length);
buf.writeBytes(proxyMessage.getData());
realServerChannel.writeAndFlush(buf);