From 0b187a852c5d86e46216ff9d73ee6be54536c2e5 Mon Sep 17 00:00:00 2001 From: Liam Date: Sat, 13 Apr 2024 16:24:49 +0100 Subject: [PATCH] check the mojang api statuses in parallel --- src/main/java/cc/fascinated/Main.java | 3 ++ .../cc/fascinated/service/MojangService.java | 44 ++++++++++++++----- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/main/java/cc/fascinated/Main.java b/src/main/java/cc/fascinated/Main.java index 71a2a08..c77a692 100644 --- a/src/main/java/cc/fascinated/Main.java +++ b/src/main/java/cc/fascinated/Main.java @@ -12,6 +12,8 @@ import java.net.http.HttpClient; import java.nio.file.Files; import java.nio.file.StandardCopyOption; import java.util.Objects; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; @Log4j2 @SpringBootApplication @@ -20,6 +22,7 @@ public class Main { .setDateFormat("MM-dd-yyyy HH:mm:ss") .create(); public static final HttpClient HTTP_CLIENT = HttpClient.newHttpClient(); + public static final ExecutorService EXECUTOR_POOL = Executors.newFixedThreadPool(8); // Adjust the thread pool size as per your requirement @SneakyThrows public static void main(String[] args) { diff --git a/src/main/java/cc/fascinated/service/MojangService.java b/src/main/java/cc/fascinated/service/MojangService.java index 97a5e95..db667e4 100644 --- a/src/main/java/cc/fascinated/service/MojangService.java +++ b/src/main/java/cc/fascinated/service/MojangService.java @@ -1,5 +1,6 @@ package cc.fascinated.service; +import cc.fascinated.Main; import cc.fascinated.common.EndpointStatus; import cc.fascinated.common.ExpiringSet; import cc.fascinated.common.WebRequest; @@ -26,6 +27,7 @@ import java.io.InputStream; import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.*; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; @Service @Log4j2 @Getter @@ -180,9 +182,9 @@ public class MojangService { } /** - * Gets the status of the Mojang API. + * Gets the status of the Mojang APIs. * - * @return the status of the Mojang API + * @return the status */ public CachedEndpointStatus getMojangApiStatus() { log.info("Getting Mojang API status"); @@ -192,18 +194,40 @@ public class MojangService { return endpointStatus.get(); } - // Fetch the status of the Mojang APIs - Map endpoints = new HashMap<>(); + // Fetch the status of the Mojang API endpoints + List> futures = new ArrayList<>(); for (EndpointStatus endpoint : MOJANG_ENDPOINTS) { - boolean online = false; - ResponseEntity response = WebRequest.get(endpoint.getEndpoint(), String.class); - if (endpoint.getAllowedStatuses().contains(response.getStatusCode())) { - online = true; - } + CompletableFuture future = CompletableFuture.supplyAsync(() -> { + boolean online = false; + ResponseEntity response = WebRequest.get(endpoint.getEndpoint(), String.class); + if (endpoint.getAllowedStatuses().contains(response.getStatusCode())) { + online = true; + } + return online; + }, Main.EXECUTOR_POOL); + + futures.add(future); + } + + // + CompletableFuture allFutures = CompletableFuture.allOf( + futures.toArray(new CompletableFuture[0])); + + try { + allFutures.get(5, TimeUnit.SECONDS); // Timeout in 10 seconds + } catch (Exception e) { + log.error("Timeout while fetching Mojang API status: {}", e.getMessage()); + } + + // Process the results + Map endpoints = new HashMap<>(); + for (int i = 0; i < MOJANG_ENDPOINTS.size(); i++) { + EndpointStatus endpoint = MOJANG_ENDPOINTS.get(i); + boolean online = futures.get(i).join(); endpoints.put(endpoint.getEndpoint(), online); } - log.info("Fetched Mojang API status for {} endpoints", endpoints.size()); + log.info("Fetched Mojang API status for {} endpoints", endpoints.size()); CachedEndpointStatus status = new CachedEndpointStatus( MOJANG_ENDPOINT_STATUS_KEY, endpoints,