perf[server]: max packet length limit

This commit is contained in:
godotg
2023-12-10 22:04:20 +08:00
parent 3454805796
commit a406596898
6 changed files with 16 additions and 29 deletions
@@ -20,7 +20,7 @@ import com.zfoo.net.packet.EncodedPacketInfo;
import com.zfoo.net.packet.PacketService;
import com.zfoo.protocol.ProtocolManager;
import com.zfoo.protocol.buffer.ByteBufUtils;
import com.zfoo.protocol.util.IOUtils;
import com.zfoo.protocol.util.MathSafeUtils;
import com.zfoo.protocol.util.StringUtils;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
@@ -47,7 +47,7 @@ public class JProtobufTcpCodecHandler extends ByteToMessageCodec<EncodedPacketIn
var length = in.readInt();
// 如果长度非法,则抛出异常断开连接,按照自己的使用场景指定合适的长度,防止客户端发送超大包占用带宽
if (length < 0 || length > IOUtils.BYTES_PER_MB) {
if (length < 0 || length > MathSafeUtils.MAX_LENGTH) {
throw new IllegalArgumentException(StringUtils.format("illegal packet [length:{}]", length));
}
@@ -14,18 +14,13 @@
package com.zfoo.net.handler.codec.tcp;
import com.zfoo.net.NetContext;
import com.zfoo.net.packet.DecodedPacketInfo;
import com.zfoo.net.packet.EncodedPacketInfo;
import com.zfoo.net.packet.PacketService;
import com.zfoo.net.util.SessionUtils;
import com.zfoo.protocol.util.IOUtils;
import com.zfoo.protocol.util.MathSafeUtils;
import com.zfoo.protocol.util.StringUtils;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageCodec;
import io.netty.util.ReferenceCountUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
@@ -47,7 +42,7 @@ public class TcpCodecHandler extends ByteToMessageCodec<EncodedPacketInfo> {
var length = in.readInt();
// 如果长度非法,则抛出异常断开连接,按照自己的使用场景指定合适的长度,防止客户端发送超大包占用带宽
if (length < 0 || length > IOUtils.BYTES_PER_MB) {
if (length < 0 || length > MathSafeUtils.MAX_LENGTH) {
throw new IllegalArgumentException(StringUtils.format("illegal packet [length:{}]", length));
}
@@ -13,20 +13,15 @@
package com.zfoo.net.handler.codec.udp;
import com.zfoo.net.NetContext;
import com.zfoo.net.packet.DecodedPacketInfo;
import com.zfoo.net.packet.EncodedPacketInfo;
import com.zfoo.net.packet.PacketService;
import com.zfoo.net.router.attachment.UdpAttachment;
import com.zfoo.protocol.util.IOUtils;
import com.zfoo.protocol.util.JsonUtils;
import com.zfoo.protocol.util.MathSafeUtils;
import com.zfoo.protocol.util.StringUtils;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.socket.DatagramPacket;
import io.netty.handler.codec.MessageToMessageCodec;
import io.netty.util.ReferenceCountUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.InetSocketAddress;
import java.util.List;
@@ -49,7 +44,7 @@ public class UdpCodecHandler extends MessageToMessageCodec<DatagramPacket, Encod
var length = in.readInt();
// 如果长度非法,则抛出异常断开连接,按照自己的使用场景指定合适的长度,防止客户端发送超大包占用带宽
if (length < 0 || length > IOUtils.BYTES_PER_MB) {
if (length < 0 || length > MathSafeUtils.MAX_LENGTH) {
throw new IllegalArgumentException(StringUtils.format("illegal packet [length:{}]", length));
}
@@ -14,20 +14,14 @@
package com.zfoo.net.handler.codec.websocket;
import com.zfoo.net.NetContext;
import com.zfoo.net.packet.DecodedPacketInfo;
import com.zfoo.net.packet.EncodedPacketInfo;
import com.zfoo.net.packet.PacketService;
import com.zfoo.protocol.util.IOUtils;
import com.zfoo.protocol.util.JsonUtils;
import com.zfoo.protocol.util.MathSafeUtils;
import com.zfoo.protocol.util.StringUtils;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageCodec;
import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketFrame;
import io.netty.util.ReferenceCountUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
@@ -44,7 +38,7 @@ public class WebSocketCodecHandler extends MessageToMessageCodec<WebSocketFrame,
ByteBuf in = webSocketFrame.content();
var length = in.readInt();
// 如果长度非法,则抛出异常断开连接,按照自己的使用场景指定合适的长度,防止客户端发送超大包占用带宽
if (length < 0 || length > IOUtils.BYTES_PER_MB) {
if (length < 0 || length > MathSafeUtils.MAX_LENGTH) {
throw new IllegalArgumentException(StringUtils.format("illegal packet [length:{}]", length));
}
var sliceByteBuf = in.readSlice(length);
@@ -13,10 +13,8 @@
package com.zfoo.protocol.collection;
import com.zfoo.protocol.model.Pair;
import com.zfoo.protocol.util.AssertionUtils;
import com.zfoo.protocol.util.IOUtils;
import com.zfoo.protocol.util.MathSafeUtil;
import com.zfoo.protocol.util.MathSafeUtils;
import com.zfoo.protocol.util.StringUtils;
import java.util.*;
@@ -116,7 +114,7 @@ public abstract class CollectionUtils {
* CN: 计算HashMap初始化合适的大小,为了安全必须给初始化的集合一个最大上限,防止反序列化一个不合法的包导致内存突然升高
*/
public static int comfortableCapacity(int capacity) {
return MathSafeUtil.safeFindNextPositivePowerOfTwo(capacity);
return MathSafeUtils.safeFindNextPositivePowerOfTwo(capacity);
}
public static int capacity(int expectedSize) {
@@ -3,7 +3,12 @@ package com.zfoo.protocol.util;
/**
* @author Boone Jack
*/
public abstract class MathSafeUtil {
public abstract class MathSafeUtils {
public static long MAX_LENGTH = IOUtils.BYTES_PER_MB;
public static long MAX_LENGTH_SHORT_ARRAY = MAX_LENGTH / 2;
public static long MAX_LENGTH_INT_ARRAY = MAX_LENGTH / 4;
public static long MAX_LENGTH_LONG_ARRAY = MAX_LENGTH / 8;
public static int findNextPositivePowerOfTwo(int value) {
assert value > Integer.MIN_VALUE && value < IOUtils.BYTES_PER_MB;