Skip to content

Commit 99bd030

Browse files
Implement missing writabilityChanged() and add backlog logging with BACKPRESSURE_LOG to all writabilityChanged() implementations. (#1745)
1 parent 5017f8c commit 99bd030

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

proxy/src/main/java/com/velocitypowered/proxy/connection/backend/ConfigSessionHandler.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import com.velocitypowered.proxy.protocol.util.PluginMessageUtil;
6161
import io.netty.buffer.ByteBufUtil;
6262
import io.netty.buffer.Unpooled;
63+
import io.netty.channel.Channel;
6364
import java.io.IOException;
6465
import java.net.InetSocketAddress;
6566
import java.util.concurrent.CompletableFuture;
@@ -72,6 +73,9 @@
7273
* 1.20.2+ switching. Yes, some of this is exceptionally stupid.
7374
*/
7475
public class ConfigSessionHandler implements MinecraftSessionHandler {
76+
private static final boolean BACKPRESSURE_LOG =
77+
Boolean.getBoolean("velocity.log-server-backpressure");
78+
7579
private static final Logger logger = LogManager.getLogger(ConfigSessionHandler.class);
7680
private final VelocityServer server;
7781
private final VelocityServerConnection serverConn;
@@ -382,6 +386,22 @@ public void handleGeneric(MinecraftPacket packet) {
382386
serverConn.getPlayer().getConnection().write(packet);
383387
}
384388

389+
@Override
390+
public void writabilityChanged() {
391+
Channel serverChan = serverConn.ensureConnected().getChannel();
392+
boolean writable = serverChan.isWritable();
393+
394+
if (BACKPRESSURE_LOG) {
395+
if (writable) {
396+
logger.info("{} is writable, will auto-read player connection data", this.serverConn);
397+
} else {
398+
logger.info("{} is not writable, not auto-reading player connection data", this.serverConn);
399+
}
400+
}
401+
402+
serverConn.getPlayer().getConnection().setAutoReading(writable);
403+
}
404+
385405
private void switchFailure(Throwable cause) {
386406
logger.error("Unable to switch to new server {} for {}", serverConn.getServerInfo().getName(),
387407
serverConn.getPlayer().getUsername(), cause);

proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientConfigSessionHandler.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@
6060
* Handles the client config stage.
6161
*/
6262
public class ClientConfigSessionHandler implements MinecraftSessionHandler {
63+
private static final boolean BACKPRESSURE_LOG =
64+
Boolean.getBoolean("velocity.log-server-backpressure");
6365

6466
private static final Logger logger = LogManager.getLogger(ClientConfigSessionHandler.class);
6567
private final VelocityServer server;
@@ -268,6 +270,33 @@ public void exception(Throwable throwable) {
268270
player.disconnect(Component.translatable("velocity.error.player-connection-error", NamedTextColor.RED));
269271
}
270272

273+
@Override
274+
public void writabilityChanged() {
275+
final boolean writable = player.getConnection().getChannel().isWritable();
276+
277+
if (BACKPRESSURE_LOG) {
278+
if (writable) {
279+
logger.info("{} is writable, will auto-read backend connection data", player);
280+
} else {
281+
logger.info("{} is not writable, not auto-reading backend connection data", player);
282+
}
283+
}
284+
285+
if (!writable) {
286+
// Flush pending packets to free up memory. Schedule on a future event loop invocation
287+
// to avoid disabling auto-read while the flush resolves backpressure.
288+
player.getConnection().eventLoop().execute(() -> player.getConnection().flush());
289+
}
290+
291+
final VelocityServerConnection serverConn = player.getConnectionInFlightOrConnectedServer();
292+
if (serverConn != null) {
293+
final MinecraftConnection smc = serverConn.getConnection();
294+
if (smc != null) {
295+
smc.setAutoReading(writable);
296+
}
297+
}
298+
}
299+
271300
/**
272301
* Calls the {@link PlayerConfigurationEvent}.
273302
* For 1.20.5+ backends this is done when the client responds to

proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@
9898
* center that joins backend servers with players.
9999
*/
100100
public class ClientPlaySessionHandler implements MinecraftSessionHandler {
101+
private static final boolean BACKPRESSURE_LOG =
102+
Boolean.getBoolean("velocity.log-server-backpressure");
101103

102104
private static final Logger logger = LogManager.getLogger(ClientPlaySessionHandler.class);
103105

@@ -505,6 +507,14 @@ public void exception(Throwable throwable) {
505507
public void writabilityChanged() {
506508
boolean writable = player.getConnection().getChannel().isWritable();
507509

510+
if (BACKPRESSURE_LOG) {
511+
if (writable) {
512+
logger.info("{} is writable, will auto-read backend connection data", player);
513+
} else {
514+
logger.info("{} is not writable, not auto-reading backend connection data", player);
515+
}
516+
}
517+
508518
if (!writable) {
509519
// We might have packets queued from the server, so flush them now to free up memory. Make
510520
// sure to do it on a future invocation of the event loop, otherwise while the issue will

0 commit comments

Comments
 (0)