From 9c4a39641d303dfc46f5757abaa5701598253f8e Mon Sep 17 00:00:00 2001 From: Rainnny7 Date: Sat, 13 Apr 2024 13:04:43 -0400 Subject: [PATCH] Run #fetchMojangServerStatuses in parallel --- .../me/braydon/mc/model/cache/CachedMinecraftServer.java | 3 ++- src/main/java/me/braydon/mc/service/MojangService.java | 8 +++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/main/java/me/braydon/mc/model/cache/CachedMinecraftServer.java b/src/main/java/me/braydon/mc/model/cache/CachedMinecraftServer.java index bfab077..c340cf7 100644 --- a/src/main/java/me/braydon/mc/model/cache/CachedMinecraftServer.java +++ b/src/main/java/me/braydon/mc/model/cache/CachedMinecraftServer.java @@ -23,6 +23,7 @@ */ package me.braydon.mc.model.cache; +import com.fasterxml.jackson.annotation.JsonBackReference; import com.fasterxml.jackson.annotation.JsonIgnore; import lombok.*; import me.braydon.mc.model.MinecraftServer; @@ -45,7 +46,7 @@ public final class CachedMinecraftServer implements Serializable { /** * The cached server. */ - @NonNull private final MinecraftServer value; + @JsonBackReference @NonNull private final MinecraftServer value; /** * The unix timestamp of when this diff --git a/src/main/java/me/braydon/mc/service/MojangService.java b/src/main/java/me/braydon/mc/service/MojangService.java index 03ea8b2..1a3b830 100644 --- a/src/main/java/me/braydon/mc/service/MojangService.java +++ b/src/main/java/me/braydon/mc/service/MojangService.java @@ -520,14 +520,12 @@ public final class MojangService { @SneakyThrows private void fetchMojangServerStatuses() { log.info("Checking Mojang server statuses..."); - long before = System.currentTimeMillis(); - for (MojangServer server : MojangServer.values()) { + Arrays.stream(MojangServer.values()).parallel().forEach(server -> { log.info("Pinging {}...", server.getEndpoint()); MojangServer.Status status = server.getStatus(); // Retrieve the server status log.info("Retrieved status of {}: {}", server.getEndpoint(), status.name()); mojangServerStatuses.put(server, status); // Cache the server status - } - log.info("Mojang server status check took {}ms", System.currentTimeMillis() - before); + }); } /** @@ -561,7 +559,7 @@ public final class MojangService { return true; } String hashed = Hashing.sha1().hashBytes(hostname.toLowerCase().getBytes(StandardCharsets.ISO_8859_1)).toString(); - boolean blocked = bannedServerHashes.contains(hashed); // Is the hostname blocked? + boolean blocked = bannedServerHashes != null && (bannedServerHashes.contains(hashed)); // Is the hostname blocked? if (blocked) { // Cache the blocked hostname blockedServersCache.add(hostname); }