From 348edfd1efe3a08243a155965db06aa17051bbd7 Mon Sep 17 00:00:00 2001 From: Liam Date: Sat, 13 Apr 2024 16:46:45 +0100 Subject: [PATCH] add a status instead of a boolean online / offline for the endpoint status --- .../model/cache/CachedEndpointStatus.java | 8 +++++++- .../cc/fascinated/service/MojangService.java | 16 ++++++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/main/java/cc/fascinated/model/cache/CachedEndpointStatus.java b/src/main/java/cc/fascinated/model/cache/CachedEndpointStatus.java index c4071a2..3e8f224 100644 --- a/src/main/java/cc/fascinated/model/cache/CachedEndpointStatus.java +++ b/src/main/java/cc/fascinated/model/cache/CachedEndpointStatus.java @@ -21,11 +21,17 @@ public final class CachedEndpointStatus implements Serializable { /** * The list of endpoints and their status. */ - private final Map endpoints; + private final Map endpoints; /** * The unix timestamp of when this * server was cached, -1 if not cached. */ private long cached; + + public enum Status { + ONLINE, + DEGRADED, + OFFLINE + } } \ No newline at end of file diff --git a/src/main/java/cc/fascinated/service/MojangService.java b/src/main/java/cc/fascinated/service/MojangService.java index d4f14b3..6324e32 100644 --- a/src/main/java/cc/fascinated/service/MojangService.java +++ b/src/main/java/cc/fascinated/service/MojangService.java @@ -195,15 +195,19 @@ public class MojangService { } // Fetch the status of the Mojang API endpoints - List> futures = new ArrayList<>(); + List> futures = new ArrayList<>(); for (EndpointStatus endpoint : MOJANG_ENDPOINTS) { - CompletableFuture future = CompletableFuture.supplyAsync(() -> { + CompletableFuture future = CompletableFuture.supplyAsync(() -> { boolean online = false; + long start = System.currentTimeMillis(); ResponseEntity response = WebRequest.head(endpoint.getEndpoint(), String.class); if (endpoint.getAllowedStatuses().contains(response.getStatusCode())) { online = true; } - return online; + if (online && System.currentTimeMillis() - start > 500) { // If the response took longer than 500ms + return CachedEndpointStatus.Status.DEGRADED; + } + return online ? CachedEndpointStatus.Status.ONLINE : CachedEndpointStatus.Status.OFFLINE; }, Main.EXECUTOR_POOL); futures.add(future); @@ -216,11 +220,11 @@ public class MojangService { } // Process the results - Map endpoints = new HashMap<>(); + 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); + CachedEndpointStatus.Status status = futures.get(i).join(); + endpoints.put(endpoint.getEndpoint(), status); } log.info("Fetched Mojang API status for {} endpoints", endpoints.size());