check the mojang api statuses in parallel

This commit is contained in:
Lee 2024-04-13 16:24:49 +01:00
parent 4d4e8557d8
commit 0b187a852c
2 changed files with 37 additions and 10 deletions

View File

@ -12,6 +12,8 @@ import java.net.http.HttpClient;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@Log4j2 @Log4j2
@SpringBootApplication @SpringBootApplication
@ -20,6 +22,7 @@ public class Main {
.setDateFormat("MM-dd-yyyy HH:mm:ss") .setDateFormat("MM-dd-yyyy HH:mm:ss")
.create(); .create();
public static final HttpClient HTTP_CLIENT = HttpClient.newHttpClient(); 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 @SneakyThrows
public static void main(String[] args) { public static void main(String[] args) {

View File

@ -1,5 +1,6 @@
package cc.fascinated.service; package cc.fascinated.service;
import cc.fascinated.Main;
import cc.fascinated.common.EndpointStatus; import cc.fascinated.common.EndpointStatus;
import cc.fascinated.common.ExpiringSet; import cc.fascinated.common.ExpiringSet;
import cc.fascinated.common.WebRequest; import cc.fascinated.common.WebRequest;
@ -26,6 +27,7 @@ import java.io.InputStream;
import java.net.URL; import java.net.URL;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@Service @Log4j2 @Getter @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() { public CachedEndpointStatus getMojangApiStatus() {
log.info("Getting Mojang API status"); log.info("Getting Mojang API status");
@ -192,18 +194,40 @@ public class MojangService {
return endpointStatus.get(); return endpointStatus.get();
} }
// Fetch the status of the Mojang APIs // Fetch the status of the Mojang API endpoints
Map<String, Boolean> endpoints = new HashMap<>(); List<CompletableFuture<Boolean>> futures = new ArrayList<>();
for (EndpointStatus endpoint : MOJANG_ENDPOINTS) { for (EndpointStatus endpoint : MOJANG_ENDPOINTS) {
CompletableFuture<Boolean> future = CompletableFuture.supplyAsync(() -> {
boolean online = false; boolean online = false;
ResponseEntity<?> response = WebRequest.get(endpoint.getEndpoint(), String.class); ResponseEntity<?> response = WebRequest.get(endpoint.getEndpoint(), String.class);
if (endpoint.getAllowedStatuses().contains(response.getStatusCode())) { if (endpoint.getAllowedStatuses().contains(response.getStatusCode())) {
online = true; online = true;
} }
return online;
}, Main.EXECUTOR_POOL);
futures.add(future);
}
//
CompletableFuture<Void> 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<String, Boolean> 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); 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( CachedEndpointStatus status = new CachedEndpointStatus(
MOJANG_ENDPOINT_STATUS_KEY, MOJANG_ENDPOINT_STATUS_KEY,
endpoints, endpoints,