From 871319d679c349878274d8d0d634ee60d523d0fb Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Sat, 15 Sep 2018 01:26:54 -0400 Subject: [PATCH] Allow custom connection and read timeouts. --- .../proxy/config/VelocityConfiguration.java | 58 +++++++++---------- .../backend/VelocityServerConnection.java | 6 +- .../proxy/network/ConnectionManager.java | 7 ++- .../proxy/network/Connections.java | 4 -- 4 files changed, 34 insertions(+), 41 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/config/VelocityConfiguration.java b/proxy/src/main/java/com/velocitypowered/proxy/config/VelocityConfiguration.java index 470e064f8..feb076dbb 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/config/VelocityConfiguration.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/config/VelocityConfiguration.java @@ -273,6 +273,14 @@ public class VelocityConfiguration extends AnnotatedConfig { return announceForge; } + public int getConnectTimeout() { + return advanced.getConnectionTimeout(); + } + + public int getReadTimeout() { + return advanced.getReadTimeout(); + } + @Override public String toString() { return MoreObjects.toStringHelper(this) @@ -420,21 +428,23 @@ public class VelocityConfiguration extends AnnotatedConfig { "Disable by setting to 0"}) @ConfigKey("login-ratelimit") private int loginRatelimit = 3000; + @Comment({"Specify a custom timeout for connection timeouts here. The default is five seconds."}) + @ConfigKey("connection-timeout") + private int connectionTimeout = 5000; + @Comment({"Specify a read timeout for connections here. The default is 30 seconds."}) + @ConfigKey("read-timeout") + private int readTimeout = 30000; private Advanced() { } - private Advanced(int compressionThreshold, int compressionLevel, int loginRatelimit) { - this.compressionThreshold = compressionThreshold; - this.compressionLevel = compressionLevel; - this.loginRatelimit = loginRatelimit; - } - private Advanced(Toml toml) { if (toml != null) { this.compressionThreshold = toml.getLong("compression-threshold", 1024L).intValue(); this.compressionLevel = toml.getLong("compression-level", -1L).intValue(); this.loginRatelimit = toml.getLong("login-ratelimit", 3000L).intValue(); + this.connectionTimeout = toml.getLong("connection-timeout", 5000L).intValue(); + this.readTimeout = toml.getLong("read-timeout", 30000L).intValue(); } } @@ -442,33 +452,31 @@ public class VelocityConfiguration extends AnnotatedConfig { return compressionThreshold; } - public void setCompressionThreshold(int compressionThreshold) { - this.compressionThreshold = compressionThreshold; - } - public int getCompressionLevel() { return compressionLevel; } - public void setCompressionLevel(int compressionLevel) { - this.compressionLevel = compressionLevel; - } - public int getLoginRatelimit() { return loginRatelimit; } - public void setLoginRatelimit(int loginRatelimit) { - this.loginRatelimit = loginRatelimit; + public int getConnectionTimeout() { + return connectionTimeout; + } + + public int getReadTimeout() { + return readTimeout; } @Override public String toString() { - return "Advanced{" - + "compressionThreshold=" + compressionThreshold - + ", compressionLevel=" + compressionLevel - + ", loginRatelimit=" + loginRatelimit - + '}'; + return "Advanced{" + + "compressionThreshold=" + compressionThreshold + + ", compressionLevel=" + compressionLevel + + ", loginRatelimit=" + loginRatelimit + + ", connectionTimeout=" + connectionTimeout + + ", readTimeout=" + readTimeout + + '}'; } } @@ -500,18 +508,10 @@ public class VelocityConfiguration extends AnnotatedConfig { return queryEnabled; } - public void setQueryEnabled(boolean queryEnabled) { - this.queryEnabled = queryEnabled; - } - public int getQueryPort() { return queryPort; } - public void setQueryPort(int queryPort) { - this.queryPort = queryPort; - } - @Override public String toString() { return "Query{" 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 0d0a4be0a..2362a27d5 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 @@ -33,8 +33,6 @@ import static com.velocitypowered.proxy.network.Connections.HANDLER; import static com.velocitypowered.proxy.network.Connections.MINECRAFT_DECODER; import static com.velocitypowered.proxy.network.Connections.MINECRAFT_ENCODER; import static com.velocitypowered.proxy.network.Connections.READ_TIMEOUT; -import static com.velocitypowered.proxy.network.Connections.CONNECTION_TIMEOUT_SECONDS; -import static com.velocitypowered.proxy.network.Connections.SERVER_READ_TIMEOUT_SECONDS; public class VelocityServerConnection implements MinecraftConnectionAssociation, ServerConnection { static final AttributeKey> CONNECTION_NOTIFIER = @@ -56,13 +54,11 @@ public class VelocityServerConnection implements MinecraftConnectionAssociation, public CompletableFuture connect() { CompletableFuture result = new CompletableFuture<>(); server.initializeGenericBootstrap() - .option(ChannelOption.TCP_NODELAY, true) - .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, CONNECTION_TIMEOUT_SECONDS * 1000) .handler(new ChannelInitializer() { @Override protected void initChannel(Channel ch) throws Exception { ch.pipeline() - .addLast(READ_TIMEOUT, new ReadTimeoutHandler(SERVER_READ_TIMEOUT_SECONDS, TimeUnit.SECONDS)) + .addLast(READ_TIMEOUT, new ReadTimeoutHandler(server.getConfiguration().getReadTimeout(), TimeUnit.SECONDS)) .addLast(FRAME_DECODER, new MinecraftVarintFrameDecoder()) .addLast(FRAME_ENCODER, MinecraftVarintLengthEncoder.INSTANCE) .addLast(MINECRAFT_DECODER, new MinecraftDecoder(ProtocolConstants.Direction.CLIENTBOUND)) 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 e1ffe129e..9a115a608 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/ConnectionManager.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/ConnectionManager.java @@ -75,7 +75,7 @@ public final class ConnectionManager { @Override protected void initChannel(final Channel ch) { ch.pipeline() - .addLast(READ_TIMEOUT, new ReadTimeoutHandler(CLIENT_READ_TIMEOUT_SECONDS, TimeUnit.SECONDS)) + .addLast(READ_TIMEOUT, new ReadTimeoutHandler(server.getConfiguration().getReadTimeout(), TimeUnit.SECONDS)) .addLast(LEGACY_PING_DECODER, new LegacyPingDecoder()) .addLast(FRAME_DECODER, new MinecraftVarintFrameDecoder()) .addLast(LEGACY_PING_ENCODER, LegacyPingEncoder.INSTANCE) @@ -89,7 +89,6 @@ public final class ConnectionManager { ch.pipeline().addLast(Connections.HANDLER, connection); } }) - .childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, CONNECTION_TIMEOUT_SECONDS * 1000) .childOption(ChannelOption.TCP_NODELAY, true) .childOption(ChannelOption.IP_TOS, 0x18) .localAddress(address); @@ -126,7 +125,9 @@ public final class ConnectionManager { public Bootstrap createWorker() { return new Bootstrap() .channel(this.transportType.socketChannelClass) - .group(this.workerGroup); + .group(this.workerGroup) + .option(ChannelOption.TCP_NODELAY, true) + .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, server.getConfiguration().getConnectTimeout()); } public void shutdown() { diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/Connections.java b/proxy/src/main/java/com/velocitypowered/proxy/network/Connections.java index 59869eede..fb248dc6a 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/Connections.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/Connections.java @@ -13,8 +13,4 @@ public interface Connections { String MINECRAFT_DECODER = "minecraft-decoder"; String MINECRAFT_ENCODER = "minecraft-encoder"; String READ_TIMEOUT = "read-timeout"; - - int CLIENT_READ_TIMEOUT_SECONDS = 30; // client -> proxy - int SERVER_READ_TIMEOUT_SECONDS = 30; // proxy -> server - int CONNECTION_TIMEOUT_SECONDS = 5; }