Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidate flushes #3785

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions proxy/src/main/java/net/md_5/bungee/ServerConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ public void handle(PacketWrapper packet) throws Exception
public void handle(LoginSuccess loginSuccess) throws Exception
{
Preconditions.checkState( thisState == State.LOGIN_SUCCESS, "Not expecting LOGIN_SUCCESS" );
user.getCh().setConsolidate( false );
if ( user.getPendingConnection().getVersion() >= ProtocolConstants.MINECRAFT_1_20_2 )
{
ServerConnection server = new ServerConnection( ch, target );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -799,9 +799,21 @@ public void handle(Login login) throws Exception
receivedLogin = true;
ServerConnector.handleLogin( bungee, server.getCh(), con, server.getInfo(), null, server, login );

con.getCh().setConsolidate( true );
server.getCh().setConsolidate( true );

throw CancelSendSignal.INSTANCE;
}

@Override
public void channelReadComplete(ChannelWrapper channel) throws Exception
{
if ( con.isConnected() )
{
con.getCh().forceFlush();
}
}

@Override
public String toString()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,15 @@ public void handle(LoginPayloadResponse loginPayloadResponse) throws Exception
con.getPendingConnection().handle( loginPayloadResponse );
}

@Override
public void channelReadComplete(ChannelWrapper channel) throws Exception
{
if ( con.getServer() != null && con.getServer().isConnected() )
{
con.getServer().getCh().forceFlush();
}
}

@Override
public String toString()
{
Expand Down
45 changes: 43 additions & 2 deletions proxy/src/main/java/net/md_5/bungee/netty/ChannelWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

public class ChannelWrapper
{
private static final int MAX_CONSOLIDATION = Integer.getInteger( "net.md_5.bungee.flush-consolidation-limit", 20 );

private final Channel ch;
@Getter
Expand All @@ -33,13 +34,21 @@ public class ChannelWrapper
private volatile boolean closed;
@Getter
private volatile boolean closing;
private boolean consolidate;
private int consolidationCounter;

public ChannelWrapper(ChannelHandlerContext ctx)
{
this.ch = ctx.channel();
this.remoteAddress = ( this.ch.remoteAddress() == null ) ? this.ch.parent().localAddress() : this.ch.remoteAddress();
}

public void setConsolidate(boolean enabled)
{
consolidate = enabled;
forceFlush();
}

public Protocol getDecodeProtocol()
{
return getMinecraftDecoder().getProtocol();
Expand All @@ -57,6 +66,8 @@ public Protocol getEncodeProtocol()

public void setEncodeProtocol(Protocol protocol)
{
// before changing the encoder protocol we should always flush to ensure no wrong states
forceFlush();
getMinecraftEncoder().setProtocol( protocol );
}

Expand Down Expand Up @@ -89,18 +100,27 @@ public int getEncodeVersion()

public void write(Object packet)
{
// ensure netty context to for less context schedules
// by default we are mostly in netty context anyway, but plugins can use ProxiedPlayer.unsafe().sendPacket()
// in non netty context
if ( !ch.eventLoop().inEventLoop() )
{
ch.eventLoop().execute( () -> write( packet ) );
return;
}

Comment on lines +103 to +111
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this then be best placed in impl of ProxiedPlayer.Unsafe.sendPacket?

Copy link
Collaborator Author

@Outfluencer Outfluencer Feb 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if i remove it there i need to put it at multiple positions as ServerConnection.unsafe.sendPacket InitialHandler.unsafe.sendpacket and ProxiedPlayer.unsafe.sendPacket

It was just the most quick way to do it there for me because i thought there is no disadvantage of putting it there?

Why dou you think it shouldnt be there?

if ( !closed )
{
DefinedPacket defined = null;
if ( packet instanceof PacketWrapper )
{
PacketWrapper wrapper = (PacketWrapper) packet;
wrapper.setReleased( true );
ch.writeAndFlush( wrapper.buf, ch.voidPromise() );
ch.write( wrapper.buf, ch.voidPromise() );
Copy link
Contributor

@Janmm14 Janmm14 Feb 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would put in the signalFlush call directly after both write calls instead of putting it below.
Or is there something I didn't notice

Or maybe add in a write0 method in ChannelWrapper taking care of potentially flushing?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why?

signalFlush is called guaranteed at the end of the method. Only if the encoder protocol is changed it will forceFlush and return.

defined = wrapper.packet;
} else
{
ch.writeAndFlush( packet, ch.voidPromise() );
ch.write( packet, ch.voidPromise() );
if ( packet instanceof DefinedPacket )
{
defined = (DefinedPacket) packet;
Expand All @@ -113,11 +133,32 @@ public void write(Object packet)
if ( nextProtocol != null )
{
setEncodeProtocol( nextProtocol );
return;
}
}
signalFlush();
}
}



private void signalFlush()
{
if ( !consolidate || consolidationCounter++ >= MAX_CONSOLIDATION )
{
forceFlush();
}

}

public void forceFlush()
{
ch.flush();
consolidationCounter = 0;
}



public void markClosed()
{
closed = closing = true;
Expand Down
10 changes: 10 additions & 0 deletions proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exceptio
if ( handler != null )
{
handler.writabilityChanged( channel );
channel.forceFlush();
}
}

Expand Down Expand Up @@ -142,6 +143,15 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
}
}

@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception
{
if ( handler != null )
{
handler.channelReadComplete( channel );
}
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception
{
Expand Down
4 changes: 4 additions & 0 deletions proxy/src/main/java/net/md_5/bungee/netty/PacketHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@ public void disconnected(ChannelWrapper channel) throws Exception
public void writabilityChanged(ChannelWrapper channel) throws Exception
{
}

public void channelReadComplete(ChannelWrapper channel) throws Exception
{
}
}