Skip to content

Commit e690d3f

Browse files
committed
perf[proxy]: don't send any packet when the tunnel buffer is full
1 parent 1cfe5ab commit e690d3f

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

net/src/main/java/com/zfoo/net/core/proxy/TunnelProtocolServer2Client.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,12 @@ public static TunnelPacketInfo read(ByteBuf in) {
6767

6868

6969
public void write(ByteBuf out) {
70-
try {
71-
out.ensureWritable(22);
72-
out.writerIndex(PacketService.PACKET_HEAD_LENGTH);
73-
ByteBufUtils.writeLong(out, sid);
74-
ByteBufUtils.writeLong(out, uid);
75-
out.writeBytes(retainedByteBuf);
76-
NetContext.getPacketService().writeHeaderBefore(out);
77-
} finally {
78-
ReferenceCountUtil.release(retainedByteBuf);
79-
}
70+
out.ensureWritable(22);
71+
out.writerIndex(PacketService.PACKET_HEAD_LENGTH);
72+
ByteBufUtils.writeLong(out, sid);
73+
ByteBufUtils.writeLong(out, uid);
74+
out.writeBytes(retainedByteBuf);
75+
NetContext.getPacketService().writeHeaderBefore(out);
8076
}
8177
// -----------------------------------------------------------------------------------------------------------------
8278

net/src/main/java/com/zfoo/net/core/proxy/handler/ProxyCodecHandler.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import io.netty.channel.ChannelHandlerContext;
2727
import io.netty.handler.codec.ByteToMessageCodec;
2828
import io.netty.util.ReferenceCountUtil;
29+
import org.slf4j.Logger;
30+
import org.slf4j.LoggerFactory;
2931

3032
import java.util.List;
3133

@@ -34,6 +36,8 @@
3436
*/
3537
public class ProxyCodecHandler extends ByteToMessageCodec<TunnelProtocolServer2Client> {
3638

39+
private static final Logger logger = LoggerFactory.getLogger(ProxyCodecHandler.class);
40+
3741

3842
@Override
3943
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
@@ -57,11 +61,20 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
5761

5862
if (CollectionUtils.isEmpty(TunnelServer.tunnels)) {
5963
in.readSlice(length);
64+
logger.warn("Tunnel server has no tunnels");
6065
return;
6166
}
67+
6268
var tunnel = RandomUtils.randomEle(TunnelServer.tunnels);
6369
if (!SessionUtils.isActive(tunnel)) {
6470
in.readSlice(length);
71+
logger.warn("Tunnel server has no active tunnels");
72+
return;
73+
}
74+
75+
if (!tunnel.isWritable()) {
76+
in.readSlice(length);
77+
logger.warn("Tunnel server has no writable tunnels");
6578
return;
6679
}
6780

net/src/main/java/com/zfoo/net/core/proxy/handler/TunnelServerCodecHandler.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
6060

6161
@Override
6262
protected void encode(ChannelHandlerContext ctx, TunnelProtocolServer2Client tunnelProtocol, ByteBuf out) {
63-
tunnelProtocol.write(out);
63+
try {
64+
tunnelProtocol.write(out);
65+
} finally {
66+
ReferenceCountUtil.release(tunnelProtocol.getRetainedByteBuf());
67+
}
6468
}
6569

6670
}

0 commit comments

Comments
 (0)