From d42cc4f984656e5ff394ed4c22bfebc5eeaf73eb Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Tue, 4 May 2021 18:51:01 -0400 Subject: [PATCH] Force a flush after a certain threshold of packets have been queued for sending Fixes #486. --- .../backend/BackendPlaySessionHandler.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java index d649b4c07..b3c7133cf 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java @@ -56,12 +56,16 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler { private static final Logger logger = LogManager.getLogger(BackendPlaySessionHandler.class); private static final boolean BACKPRESSURE_LOG = Boolean .getBoolean("velocity.log-server-backpressure"); + private static final int MAXIMUM_PACKETS_TO_FLUSH = Integer + .getInteger("velocity.max-packets-per-flush", 8192); + private final VelocityServer server; private final VelocityServerConnection serverConn; private final ClientPlaySessionHandler playerSessionHandler; private final MinecraftConnection playerConnection; private final BungeeCordMessageResponder bungeecordMessageResponder; private boolean exceptionTriggered = false; + private int packetsFlushed; BackendPlaySessionHandler(VelocityServer server, VelocityServerConnection serverConn) { this.server = server; @@ -271,16 +275,25 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler { ((PluginMessage) packet).retain(); } playerConnection.delayedWrite(packet); + if (++packetsFlushed >= MAXIMUM_PACKETS_TO_FLUSH) { + playerConnection.flush(); + packetsFlushed = 0; + } } @Override public void handleUnknown(ByteBuf buf) { playerConnection.delayedWrite(buf.retain()); + if (++packetsFlushed >= MAXIMUM_PACKETS_TO_FLUSH) { + playerConnection.flush(); + packetsFlushed = 0; + } } @Override public void readCompleted() { playerConnection.flush(); + packetsFlushed = 0; } @Override