add a status instead of a boolean online / offline for the endpoint status

This commit is contained in:
Lee 2024-04-13 16:46:45 +01:00
parent 66f5660274
commit 348edfd1ef
2 changed files with 17 additions and 7 deletions

@ -21,11 +21,17 @@ public final class CachedEndpointStatus implements Serializable {
/** /**
* The list of endpoints and their status. * The list of endpoints and their status.
*/ */
private final Map<String, Boolean> endpoints; private final Map<String, Status> endpoints;
/** /**
* The unix timestamp of when this * The unix timestamp of when this
* server was cached, -1 if not cached. * server was cached, -1 if not cached.
*/ */
private long cached; private long cached;
public enum Status {
ONLINE,
DEGRADED,
OFFLINE
}
} }

@ -195,15 +195,19 @@ public class MojangService {
} }
// Fetch the status of the Mojang API endpoints // Fetch the status of the Mojang API endpoints
List<CompletableFuture<Boolean>> futures = new ArrayList<>(); List<CompletableFuture<CachedEndpointStatus.Status>> futures = new ArrayList<>();
for (EndpointStatus endpoint : MOJANG_ENDPOINTS) { for (EndpointStatus endpoint : MOJANG_ENDPOINTS) {
CompletableFuture<Boolean> future = CompletableFuture.supplyAsync(() -> { CompletableFuture<CachedEndpointStatus.Status> future = CompletableFuture.supplyAsync(() -> {
boolean online = false; boolean online = false;
long start = System.currentTimeMillis();
ResponseEntity<?> response = WebRequest.head(endpoint.getEndpoint(), String.class); ResponseEntity<?> response = WebRequest.head(endpoint.getEndpoint(), String.class);
if (endpoint.getAllowedStatuses().contains(response.getStatusCode())) { if (endpoint.getAllowedStatuses().contains(response.getStatusCode())) {
online = true; 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); }, Main.EXECUTOR_POOL);
futures.add(future); futures.add(future);
@ -216,11 +220,11 @@ public class MojangService {
} }
// Process the results // Process the results
Map<String, Boolean> endpoints = new HashMap<>(); Map<String, CachedEndpointStatus.Status> endpoints = new HashMap<>();
for (int i = 0; i < MOJANG_ENDPOINTS.size(); i++) { for (int i = 0; i < MOJANG_ENDPOINTS.size(); i++) {
EndpointStatus endpoint = MOJANG_ENDPOINTS.get(i); EndpointStatus endpoint = MOJANG_ENDPOINTS.get(i);
boolean online = futures.get(i).join(); CachedEndpointStatus.Status status = futures.get(i).join();
endpoints.put(endpoint.getEndpoint(), online); endpoints.put(endpoint.getEndpoint(), status);
} }
log.info("Fetched Mojang API status for {} endpoints", endpoints.size()); log.info("Fetched Mojang API status for {} endpoints", endpoints.size());