From f4aceb00bb119c04874b1c7ef20ac3784caa2c9a Mon Sep 17 00:00:00 2001 From: e3ndr <33337309+e3ndr@users.noreply.github.com> Date: Tue, 10 Dec 2024 09:58:52 -0600 Subject: [PATCH] Try this one out. --- .../casterlabs/caffeinated/updater/Updater.java | 17 +++++++++++++++-- .../caffeinated/updater/util/WebUtil.java | 5 +---- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/main/java/co/casterlabs/caffeinated/updater/Updater.java b/src/main/java/co/casterlabs/caffeinated/updater/Updater.java index 6550d01..2476338 100644 --- a/src/main/java/co/casterlabs/caffeinated/updater/Updater.java +++ b/src/main/java/co/casterlabs/caffeinated/updater/Updater.java @@ -10,7 +10,7 @@ import java.net.http.HttpResponse; import java.net.http.HttpResponse.BodyHandlers; import java.nio.file.Files; -import java.time.Duration; +import java.util.concurrent.TimeUnit; import co.casterlabs.caffeinated.updater.target.Target; import co.casterlabs.caffeinated.updater.util.FileUtil; @@ -74,6 +74,17 @@ public static int needsUpdate() { return 1; } + Thread currentThread = Thread.currentThread(); + Thread watchdog = new Thread(() -> { + try { + currentThread.join(TimeUnit.SECONDS.toMillis(15)); + currentThread.interrupt(); + } catch (InterruptedException ignored) { + // Everything succeeded :D + } + }); + watchdog.start(); + try { JsonObject buildInfo = Rson.DEFAULT.fromJson(FileUtil.readFile(buildInfoFile), JsonObject.class); @@ -82,7 +93,6 @@ public static int needsUpdate() { String remoteCommit = WebUtil.sendHttpRequest( HttpRequest.newBuilder() .uri(URI.create(CHANNEL_COMMIT_URL)) - .timeout(Duration.ofSeconds(15)) ).trim(); if (!remoteCommit.equals(installedCommit)) return 1; @@ -91,6 +101,9 @@ public static int needsUpdate() { if (!installedChannel.equals(CHANNEL)) return 1; } catch (IOException | InterruptedException e) { return 2; + } finally { + Thread.interrupted(); // Clear interrupted status. + watchdog.interrupt(); } return 0; diff --git a/src/main/java/co/casterlabs/caffeinated/updater/util/WebUtil.java b/src/main/java/co/casterlabs/caffeinated/updater/util/WebUtil.java index 3d81fbb..16eb203 100644 --- a/src/main/java/co/casterlabs/caffeinated/updater/util/WebUtil.java +++ b/src/main/java/co/casterlabs/caffeinated/updater/util/WebUtil.java @@ -6,14 +6,11 @@ import java.net.http.HttpResponse; import java.net.http.HttpResponse.BodyHandler; import java.net.http.HttpResponse.BodyHandlers; -import java.time.Duration; import lombok.NonNull; public class WebUtil { - private static final HttpClient client = HttpClient.newBuilder() - .connectTimeout(Duration.ofSeconds(15)) - .build(); + private static final HttpClient client = HttpClient.newHttpClient(); public static HttpResponse sendRawHttpRequest(@NonNull HttpRequest.Builder builder, @NonNull BodyHandler handler) throws IOException, InterruptedException { return client.send(builder.build(), handler);