13
0
geforkt von Mirrors/Paper

Switch metrics impl to use thread executor service

Dieser Commit ist enthalten in:
Zach Brown 2017-03-26 18:29:38 -05:00
Ursprung 4340ef97ca
Commit b6fe136d35

Datei anzeigen

@ -15,7 +15,7 @@ decisions on behalf of the project.
diff --git a/src/main/java/com/destroystokyo/paper/Metrics.java b/src/main/java/com/destroystokyo/paper/Metrics.java
new file mode 100644
index 000000000..3084096c0
index 000000000..585260697
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/Metrics.java
@@ -0,0 +0,0 @@
@ -45,9 +45,10 @@ index 000000000..3084096c0
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Timer;
+import java.util.TimerTask;
+import java.util.UUID;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.logging.Level;
+import java.util.zip.GZIPOutputStream;
+
@ -56,7 +57,7 @@ index 000000000..3084096c0
+ *
+ * Check out https://bStats.org/ to learn more about bStats!
+ */
+public class Metrics {
+class Metrics {
+
+ static {
+ // Maven's Relocate is clever and changes strings, too. So we have to use this little "trick" ... :D
@ -82,6 +83,9 @@ index 000000000..3084096c0
+
+ // A list with all custom charts
+ private final List<CustomChart> charts = new ArrayList<>();
+
+ // Executor for use in scheduling work and submitting data
+ private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
+
+ /**
+ * Class constructor.
@ -138,15 +142,11 @@ index 000000000..3084096c0
+ * Starts the Scheduler which submits our data every 30 minutes.
+ */
+ private void startSubmitting() {
+ final Timer timer = new Timer(true); // We use a timer cause the Bukkit scheduler is affected by server lags
+ timer.scheduleAtFixedRate(new TimerTask() {
+ @Override
+ public void run() {
+ // Nevertheless we want our code to run in the Bukkit main thread, so we have to use the Bukkit scheduler
+ executor.scheduleAtFixedRate(() -> {
+ // Nevertheless we want our code to run in the main thread, so we have to use the MC scheduler
+ // Don't be afraid! The connection to the bStats server is still async, only the stats collection is sync ;)
+ MinecraftServer.getServer().postToMainThread(() -> submitData());
+ }
+ }, 1000*60*5, 1000*60*30);
+ MinecraftServer.getServer().postToMainThread(this::submitData);
+ }, 5, 30, TimeUnit.MINUTES);
+ // 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!
@ -225,8 +225,8 @@ index 000000000..3084096c0
+ pluginData.add(this.getPluginData());
+ data.put("plugins", pluginData);
+
+ // Create a new thread for the connection to the bStats server
+ new Thread(() -> {
+ // Post to separate thread for the connection to the bStats server
+ executor.execute(() -> {
+ try {
+ // Send the data
+ sendData(data);
@ -236,7 +236,7 @@ index 000000000..3084096c0
+ Bukkit.getLogger().log(Level.WARNING, "Could not submit stats for Paper", e);
+ }
+ }
+ }, "Paper Metrics Submission Thread").start();
+ });
+ }
+
+ /**