#3779: Improve eventloop consistency and isClosing code

This commit is contained in:
Outfluencer 2025-02-08 15:50:43 +11:00 committed by md_5
parent 2337acfcc1
commit c3e4a6ef5b
No known key found for this signature in database
GPG Key ID: E8E901AC7C617C11

View File

@ -2,6 +2,7 @@ package net.md_5.bungee.connection;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.gson.Gson; import com.google.gson.Gson;
import io.netty.channel.EventLoop;
import java.math.BigInteger; import java.math.BigInteger;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.SocketAddress; import java.net.SocketAddress;
@ -228,11 +229,6 @@ public class InitialHandler extends PacketHandler implements PendingConnection
@Override @Override
public void done(ProxyPingEvent result, Throwable error) public void done(ProxyPingEvent result, Throwable error)
{ {
if ( ch.isClosing() )
{
return;
}
ServerPing legacy = result.getResponse(); ServerPing legacy = result.getResponse();
String kickMessage; String kickMessage;
@ -256,7 +252,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection
} }
}; };
bungee.getPluginManager().callEvent( new ProxyPingEvent( InitialHandler.this, result, callback ) ); bungee.getPluginManager().callEvent( new ProxyPingEvent( InitialHandler.this, result, eventLoopCallback( callback ) ) );
} }
}; };
@ -318,7 +314,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection
} }
}; };
bungee.getPluginManager().callEvent( new ProxyPingEvent( InitialHandler.this, result, callback ) ); bungee.getPluginManager().callEvent( new ProxyPingEvent( InitialHandler.this, result, eventLoopCallback( callback ) ) );
} }
}; };
@ -484,10 +480,6 @@ public class InitialHandler extends PacketHandler implements PendingConnection
disconnect( ( reason != null ) ? reason : TextComponent.fromLegacy( bungee.getTranslation( "kick_message" ) ) ); disconnect( ( reason != null ) ? reason : TextComponent.fromLegacy( bungee.getTranslation( "kick_message" ) ) );
return; return;
} }
if ( ch.isClosing() )
{
return;
}
if ( onlineMode ) if ( onlineMode )
{ {
thisState = State.ENCRYPT; thisState = State.ENCRYPT;
@ -501,7 +493,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection
}; };
// fire pre login event // fire pre login event
bungee.getPluginManager().callEvent( new PreLoginEvent( InitialHandler.this, callback ) ); bungee.getPluginManager().callEvent( new PreLoginEvent( InitialHandler.this, eventLoopCallback( callback ) ) );
} }
@Override @Override
@ -621,18 +613,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection
disconnect( ( reason != null ) ? reason : TextComponent.fromLegacy( bungee.getTranslation( "kick_message" ) ) ); disconnect( ( reason != null ) ? reason : TextComponent.fromLegacy( bungee.getTranslation( "kick_message" ) ) );
return; return;
} }
if ( ch.isClosing() )
{
return;
}
ch.getHandle().eventLoop().execute( new Runnable()
{
@Override
public void run()
{
if ( !ch.isClosing() )
{
userCon = new UserConnection( bungee, ch, getName(), InitialHandler.this ); userCon = new UserConnection( bungee, ch, getName(), InitialHandler.this );
userCon.setCompressionThreshold( BungeeCord.getInstance().config.getCompressionThreshold() ); userCon.setCompressionThreshold( BungeeCord.getInstance().config.getCompressionThreshold() );
@ -643,13 +624,10 @@ public class InitialHandler extends PacketHandler implements PendingConnection
} }
finish2(); finish2();
} }
}
} );
}
}; };
// fire login event // fire login event
bungee.getPluginManager().callEvent( new LoginEvent( InitialHandler.this, complete ) ); bungee.getPluginManager().callEvent( new LoginEvent( InitialHandler.this, eventLoopCallback( complete ) ) );
} }
private void finish2() private void finish2()
@ -680,18 +658,12 @@ public class InitialHandler extends PacketHandler implements PendingConnection
@Override @Override
public void done(PostLoginEvent result, Throwable error) public void done(PostLoginEvent result, Throwable error)
{ {
// #3612: Don't progress further if disconnected during event
if ( ch.isClosing() )
{
return;
}
userCon.connect( result.getTarget(), null, true, ServerConnectEvent.Reason.JOIN_PROXY ); userCon.connect( result.getTarget(), null, true, ServerConnectEvent.Reason.JOIN_PROXY );
} }
}; };
// fire post-login event // fire post-login event
bungee.getPluginManager().callEvent( new PostLoginEvent( userCon, initialServer, complete ) ); bungee.getPluginManager().callEvent( new PostLoginEvent( userCon, initialServer, eventLoopCallback( complete ) ) );
} }
@Override @Override
@ -920,4 +892,26 @@ public class InitialHandler extends PacketHandler implements PendingConnection
unsafe.sendPacket( new LoginPayloadRequest( id, channel, data ) ); unsafe.sendPacket( new LoginPayloadRequest( id, channel, data ) );
return future; return future;
} }
// this method is used for event execution
// if this connection is disconnected during an event-call, the original callback is not called
// if the event was executed async, we execute the callback on the eventloop again
// otherwise netty will schedule any pipeline related call by itself, this decreases performance
private <T> Callback<T> eventLoopCallback(Callback<T> callback)
{
EventLoop eventLoop = ch.getHandle().eventLoop();
return eventLoop.inEventLoop() ? (result, error) ->
{
if ( !ch.isClosing() )
{
callback.done( result, error );
}
} : (result, error) -> eventLoop.execute( () ->
{
if ( !ch.isClosing() )
{
callback.done( result, error );
}
} );
}
} }