Adjust bStats data sending delay (#4914)

Many servers tend to restart at a fixed time at xx:00 which causes an
uneven distribution of requests on the bStats backend. To circumvent
this problem, this commit introduces some randomness to the initial
and second delay.
This commit is contained in:
Bastian Oppermann 2020-12-20 16:41:00 +01:00
parent 57c5f5e566
commit fdaae06cdf
2 changed files with 30 additions and 20 deletions

View File

@ -45,15 +45,19 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
--- a/src/main/java/com/destroystokyo/paper/Metrics.java
+++ b/src/main/java/com/destroystokyo/paper/Metrics.java
@@ -0,0 +0,0 @@ public class Metrics {
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
+ if (MinecraftServer.getServer().hasStopped()) {
+ return;
+ }
submitData();
}
}, 1000 * 60 * 5, 1000 * 60 * 30);
* Starts the Scheduler which submits our data every 30 minutes.
*/
private void startSubmitting() {
- final Runnable submitTask = this::submitData;
+ final Runnable submitTask = () -> {
+ if (MinecraftServer.getServer().hasStopped()) {
+ return;
+ }
+ submitData();
+ };
// Many servers tend to restart at a fixed time at xx:00 which causes an uneven distribution of requests on the
// bStats backend. To circumvent this problem, we introduce some randomness into the initial and second delay.
diff --git a/src/main/java/net/minecraft/server/CrashReport.java b/src/main/java/net/minecraft/server/CrashReport.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/server/CrashReport.java

View File

@ -24,7 +24,6 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+import net.minecraft.server.MinecraftServer;
+import org.bukkit.Bukkit;
+import org.bukkit.configuration.file.YamlConfiguration;
+import org.bukkit.craftbukkit.util.CraftLegacy;
+import org.bukkit.craftbukkit.util.CraftMagicNumbers;
+import org.bukkit.plugin.Plugin;
+
@ -39,6 +38,9 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+import java.net.URL;
+import java.util.*;
+import java.util.concurrent.Callable;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import java.util.regex.Matcher;
@ -52,6 +54,10 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ */
+public class Metrics {
+
+ // Executor service for requests
+ // We use an executor service because the Bukkit scheduler is affected by server lags
+ private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
+
+ // The version of this bStats class
+ public static final int B_STATS_VERSION = 1;
+
@ -107,16 +113,16 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ * Starts the Scheduler which submits our data every 30 minutes.
+ */
+ private void startSubmitting() {
+ final Timer timer = new Timer(true);
+ timer.scheduleAtFixedRate(new TimerTask() {
+ @Override
+ public void run() {
+ submitData();
+ }
+ }, 1000 * 60 * 5, 1000 * 60 * 30);
+ // Submit the data every 30 minutes, first time after 5 minutes to give other plugins enough time to start
+ // WARNING: Changing the frequency has no effect but your plugin WILL be blocked/deleted!
+ // WARNING: Just don't do it!
+ final Runnable submitTask = this::submitData;
+
+ // Many servers tend to restart at a fixed time at xx:00 which causes an uneven distribution of requests on the
+ // bStats backend. To circumvent this problem, we introduce some randomness into the initial and second delay.
+ // WARNING: You must not modify any part of this Metrics class, including the submit delay or frequency!
+ // WARNING: Modifying this code will get your plugin banned on bStats. Just don't do it!
+ long initialDelay = (long) (1000 * 60 * (3 + Math.random() * 3));
+ long secondDelay = (long) (1000 * 60 * (Math.random() * 30));
+ scheduler.schedule(submitTask, initialDelay, TimeUnit.MILLISECONDS);
+ scheduler.scheduleAtFixedRate(submitTask, initialDelay + secondDelay, 1000 * 60 * 30, TimeUnit.MILLISECONDS);
+ }
+
+ /**