diff --git a/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java b/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java index d41c11c9c..0cf29647e 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java @@ -41,6 +41,7 @@ import com.velocitypowered.proxy.util.VelocityChannelRegistrar; import com.velocitypowered.proxy.util.ratelimit.Ratelimiter; import com.velocitypowered.proxy.util.ratelimit.Ratelimiters; import io.netty.bootstrap.Bootstrap; +import io.netty.channel.EventLoopGroup; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.file.Files; @@ -239,6 +240,10 @@ public class VelocityServer implements ProxyServer { return this.cm.createWorker(); } + public Bootstrap initializeGenericBootstrap(EventLoopGroup group) { + return this.cm.createWorker(group); + } + public boolean isShutdown() { return shutdown; } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java index b2afac997..51f80d285 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java @@ -71,7 +71,9 @@ public class VelocityServerConnection implements MinecraftConnectionAssociation, */ public CompletableFuture connect() { CompletableFuture result = new CompletableFuture<>(); - server.initializeGenericBootstrap() + // Note: we use the event loop for the connection the player is on. This reduces context + // switches. + server.initializeGenericBootstrap(proxyPlayer.getMinecraftConnection().eventLoop()) .handler(new ChannelInitializer() { @Override protected void initChannel(Channel ch) throws Exception { diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/ConnectionManager.java b/proxy/src/main/java/com/velocitypowered/proxy/network/ConnectionManager.java index 4d7d7c9e1..520a09f13 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/ConnectionManager.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/ConnectionManager.java @@ -108,15 +108,21 @@ public final class ConnectionManager { }); } + public Bootstrap createWorker() { + return this.createWorker(this.workerGroup); + } + /** * Creates a TCP {@link Bootstrap} using Velocity's event loops. * + * @param group the event loop group to use + * * @return a new {@link Bootstrap} */ - public Bootstrap createWorker() { + public Bootstrap createWorker(EventLoopGroup group) { return new Bootstrap() .channel(this.transportType.socketChannelClass) - .group(this.workerGroup) + .group(group) .option(ChannelOption.TCP_NODELAY, true) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, this.server.getConfiguration().getConnectTimeout())