Seperate client/serverbound payload limits

This commit is contained in:
Shane Freeder
2026-04-17 21:03:04 +01:00
parent a6d97e28ad
commit f712997dd7
2 changed files with 23 additions and 4 deletions

View File

@@ -68,6 +68,7 @@ import com.velocitypowered.proxy.protocol.packet.TransferPacket;
import com.velocitypowered.proxy.protocol.packet.UpsertPlayerInfoPacket; import com.velocitypowered.proxy.protocol.packet.UpsertPlayerInfoPacket;
import com.velocitypowered.proxy.protocol.packet.chat.ComponentHolder; import com.velocitypowered.proxy.protocol.packet.chat.ComponentHolder;
import com.velocitypowered.proxy.protocol.packet.config.StartUpdatePacket; import com.velocitypowered.proxy.protocol.packet.config.StartUpdatePacket;
import com.velocitypowered.proxy.protocol.util.DeferredByteBufHolder;
import com.velocitypowered.proxy.protocol.util.PluginMessageUtil; import com.velocitypowered.proxy.protocol.util.PluginMessageUtil;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil; import io.netty.buffer.ByteBufUtil;
@@ -91,6 +92,7 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler {
Boolean.getBoolean("velocity.log-server-backpressure"); Boolean.getBoolean("velocity.log-server-backpressure");
private static final int MAXIMUM_PACKETS_TO_FLUSH = private static final int MAXIMUM_PACKETS_TO_FLUSH =
Integer.getInteger("velocity.max-packets-per-flush", 8192); Integer.getInteger("velocity.max-packets-per-flush", 8192);
private static final int LARGE_PACKET_THRESHOLD = 1024 * 128;
private final VelocityServer server; private final VelocityServer server;
private final VelocityServerConnection serverConn; private final VelocityServerConnection serverConn;
@@ -455,8 +457,9 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler {
if (packet instanceof PluginMessagePacket pluginMessage) { if (packet instanceof PluginMessagePacket pluginMessage) {
pluginMessage.retain(); pluginMessage.retain();
} }
boolean huge = packet instanceof DeferredByteBufHolder def && def.content().readableBytes() > LARGE_PACKET_THRESHOLD;
playerConnection.delayedWrite(packet); playerConnection.delayedWrite(packet);
if (++packetsFlushed >= MAXIMUM_PACKETS_TO_FLUSH) { if (huge || ++packetsFlushed >= MAXIMUM_PACKETS_TO_FLUSH) {
playerConnection.flush(); playerConnection.flush();
packetsFlushed = 0; packetsFlushed = 0;
} }
@@ -464,8 +467,9 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler {
@Override @Override
public void handleUnknown(ByteBuf buf) { public void handleUnknown(ByteBuf buf) {
boolean huge = buf.readableBytes() > LARGE_PACKET_THRESHOLD;
playerConnection.delayedWrite(buf.retain()); playerConnection.delayedWrite(buf.retain());
if (++packetsFlushed >= MAXIMUM_PACKETS_TO_FLUSH) { if (huge || ++packetsFlushed >= MAXIMUM_PACKETS_TO_FLUSH) {
playerConnection.flush(); playerConnection.flush();
packetsFlushed = 0; packetsFlushed = 0;
} }

View File

@@ -31,7 +31,8 @@ import org.checkerframework.checker.nullness.qual.Nullable;
public class PluginMessagePacket extends DeferredByteBufHolder implements MinecraftPacket { public class PluginMessagePacket extends DeferredByteBufHolder implements MinecraftPacket {
private static final int MAX_PAYLOAD_SIZE = Integer.getInteger("velocity.max-plugin-message-payload-size", 32767); private static final int MAX_PAYLOAD_SIZE_CLIENTBOUND = getPayloadLimit(Direction.CLIENTBOUND);
private static final int MAX_PAYLOAD_SIZE_SERVERBOUND = getPayloadLimit(Direction.SERVERBOUND);
private @Nullable String channel; private @Nullable String channel;
@@ -52,6 +53,19 @@ public class PluginMessagePacket extends DeferredByteBufHolder implements Minecr
return channel; return channel;
} }
private static int getPayloadLimit(Direction direction) {
if (System.getProperty("velocity.max-plugin-message-payload-size") != null) {
return Integer.getInteger("velocity.max-plugin-message-payload-size");
}
if (direction == Direction.SERVERBOUND) {
return Integer.getInteger("velocity.max-plugin-message-payload-size.serverbound", 32767);
} else {
// This is the vanilla expected limit, a payload this large feels like a nightmare given the trust
// we give to servers...
return Integer.getInteger("velocity.max-plugin-message-payload-size.clientbound", 1048576);
}
}
public void setChannel(String channel) { public void setChannel(String channel) {
this.channel = channel; this.channel = channel;
} }
@@ -104,7 +118,8 @@ public class PluginMessagePacket extends DeferredByteBufHolder implements Minecr
@Override @Override
public int decodeExpectedMaxLength(ByteBuf buf, Direction direction, ProtocolVersion version) { public int decodeExpectedMaxLength(ByteBuf buf, Direction direction, ProtocolVersion version) {
return ProtocolUtils.DEFAULT_MAX_STRING_BYTES + MAX_PAYLOAD_SIZE; return ProtocolUtils.DEFAULT_MAX_STRING_BYTES +
(direction == Direction.CLIENTBOUND ? MAX_PAYLOAD_SIZE_CLIENTBOUND : MAX_PAYLOAD_SIZE_SERVERBOUND);
} }
@Override @Override