From 2a1e83902da2b67e440d8cd4a47588702a31997a Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Sun, 27 Dec 2020 18:05:27 -0500 Subject: [PATCH] Add support for server-side backpressure --- .../proxy/connection/MinecraftConnection.java | 4 ++++ .../connection/backend/BackendPlaySessionHandler.java | 8 ++++++++ .../proxy/connection/client/ClientPlaySessionHandler.java | 2 +- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/MinecraftConnection.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/MinecraftConnection.java index acf7fe5cc..3571f7a0f 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/MinecraftConnection.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/MinecraftConnection.java @@ -195,6 +195,8 @@ public class MinecraftConnection extends ChannelInboundHandlerAdapter { public void write(Object msg) { if (channel.isActive()) { channel.writeAndFlush(msg, channel.voidPromise()); + } else { + ReferenceCountUtil.release(msg); } } @@ -205,6 +207,8 @@ public class MinecraftConnection extends ChannelInboundHandlerAdapter { public void delayedWrite(Object msg) { if (channel.isActive()) { channel.write(msg, channel.voidPromise()); + } else { + ReferenceCountUtil.release(msg); } } 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 abd1f7b91..476460dc1 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 @@ -28,6 +28,7 @@ import com.velocitypowered.proxy.protocol.util.PluginMessageUtil; import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufUtil; import io.netty.buffer.Unpooled; +import io.netty.channel.Channel; import io.netty.handler.timeout.ReadTimeoutException; import java.util.Collection; import org.apache.logging.log4j.LogManager; @@ -284,4 +285,11 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler { } } } + + @Override + public void writabilityChanged() { + Channel serverChan = serverConn.ensureConnected().getChannel(); + boolean writable = serverChan.isWritable(); + playerConnection.setAutoReading(writable); + } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java index 5757b3040..b41a5ae32 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java @@ -300,7 +300,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { boolean writable = player.getConnection().getChannel().isWritable(); if (!writable) { - // We might have packets queued for the server, so flush them now to free up memory. + // We might have packets queued from the server, so flush them now to free up memory. player.getConnection().flush(); }