Waterfall/BungeeCord-Patches/0021-Add-basic-support-for-...

99 lines
3.4 KiB
Diff

From 4b36feac0d134f081b418653e41547fb5b243081 Mon Sep 17 00:00:00 2001
From: Johannes Donath <johannesd@torchmind.com>
Date: Sat, 4 Jul 2015 06:31:33 +0200
Subject: [PATCH] Add basic support for configurable tab-complete throttling
diff --git a/api/src/main/java/net/md_5/bungee/api/ProxyConfig.java b/api/src/main/java/net/md_5/bungee/api/ProxyConfig.java
index 293ec4e..66d0b8a 100644
--- a/api/src/main/java/net/md_5/bungee/api/ProxyConfig.java
+++ b/api/src/main/java/net/md_5/bungee/api/ProxyConfig.java
@@ -88,4 +88,13 @@ public interface ProxyConfig
* If metrics is enabled
*/
boolean isMetrics();
+
+ // Throttling options
+
+ /**
+ * How often tab-complete packets can be sent.
+ * <p/>
+ * Values in milliseconds.
+ */
+ int getTabThrottle();
}
diff --git a/proxy/src/main/java/io/github/waterfallmc/waterfall/conf/WaterfallConfiguration.java b/proxy/src/main/java/io/github/waterfallmc/waterfall/conf/WaterfallConfiguration.java
index 1fa3ecd..23a5c94 100644
--- a/proxy/src/main/java/io/github/waterfallmc/waterfall/conf/WaterfallConfiguration.java
+++ b/proxy/src/main/java/io/github/waterfallmc/waterfall/conf/WaterfallConfiguration.java
@@ -17,16 +17,36 @@ public class WaterfallConfiguration extends Configuration {
*/
private boolean metrics = true;
+ /*
+ * Throttling options
+ * Helps prevent players from overloading the servers behind us
+ */
+
+ /**
+ * How often players are allowed to send tab throttle.
+ * Value in milliseconds.
+ * <p/>
+ * Default is one packet per second.
+ */
+ private int tabThrottle = 1000;
+
@Override
public void load() {
super.load();
YamlConfig config = new YamlConfig(new File("waterfall.yml"));
config.load(false); // Load, but no permissions
metrics = config.getBoolean("metrics", metrics);
+ // Throttling options
+ tabThrottle = config.getInt("throttling.tab_complete", tabThrottle);
}
@Override
public boolean isMetrics() {
return metrics;
}
+
+ @Override
+ public int getTabThrottle() {
+ return tabThrottle;
+ }
}
diff --git a/proxy/src/main/java/net/md_5/bungee/connection/UpstreamBridge.java b/proxy/src/main/java/net/md_5/bungee/connection/UpstreamBridge.java
index 23d4f25..28791a6 100644
--- a/proxy/src/main/java/net/md_5/bungee/connection/UpstreamBridge.java
+++ b/proxy/src/main/java/net/md_5/bungee/connection/UpstreamBridge.java
@@ -31,6 +31,8 @@ public class UpstreamBridge extends PacketHandler
private final ProxyServer bungee;
private final UserConnection con;
+ private long lastTabCompletion = -1;
+
public UpstreamBridge(ProxyServer bungee, UserConnection con)
{
this.bungee = bungee;
@@ -121,6 +123,16 @@ public class UpstreamBridge extends PacketHandler
@Override
public void handle(TabCompleteRequest tabComplete) throws Exception
{
+ if ( bungee.getConfig().getTabThrottle() > 0 )
+ {
+ long now = System.currentTimeMillis();
+ if ( lastTabCompletion > 0 && (now - lastTabCompletion) <= bungee.getConfig().getTabThrottle() )
+ {
+ throw CancelSendSignal.INSTANCE;
+ }
+ lastTabCompletion = now;
+ }
+
List<String> suggestions = new ArrayList<>();
if ( tabComplete.getCursor().startsWith( "/" ) )
--
2.8.3