Skip to content

Commit

Permalink
Add explicit timeout of 10s to resource-downloads to avoid stalling
Browse files Browse the repository at this point in the history
  • Loading branch information
TBlueF committed Jan 14, 2025
1 parent 91e0cdf commit b3dfc47
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@
import java.io.IOException;
import java.io.OutputStream;
import java.io.Reader;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
Expand All @@ -53,7 +54,7 @@
public class MinecraftVersion {
private static final Gson GSON = new Gson();

private static final String LATEST_KNOWN_VERSION = "1.20.6";
private static final String LATEST_KNOWN_VERSION = "1.21.4";
private static final String EARLIEST_RESOURCEPACK_VERSION = "1.13";
private static final String EARLIEST_DATAPACK_VERSION = "1.19.4";

Expand Down Expand Up @@ -143,7 +144,7 @@ private static void download(VersionManifest.Version version, Path file) throws
try {
try (
DigestInputStream in = new DigestInputStream(
new URI(download.getUrl()).toURL().openStream(),
download.createInputStream(),
MessageDigest.getInstance("SHA-1")
);
OutputStream out = Files.newOutputStream(unverifiedFile)
Expand All @@ -165,7 +166,7 @@ private static void download(VersionManifest.Version version, Path file) throws
// rename once verified
FileHelper.atomicMove(unverifiedFile, file);

} catch (NoSuchAlgorithmException | IOException | URISyntaxException ex) {
} catch (NoSuchAlgorithmException | IOException ex) {
Logger.global.logWarning("Failed to download '" + download.getUrl() + "': " + ex);
} finally {
Files.deleteIfExists(unverifiedFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import org.jetbrains.annotations.Nullable;

import java.io.*;
import java.net.URL;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.Arrays;
Expand All @@ -48,6 +48,8 @@ public class VersionManifest {
public static final String DOMAIN = "https://piston-meta.mojang.com/";
public static final String MANIFEST_URL = DOMAIN + "mc/game/version_manifest.json";

private static final int CONNECTION_TIMEOUT = 10000;

private static final Gson GSON = new GsonBuilder()
.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeAdapter())
.create();
Expand All @@ -71,7 +73,7 @@ public static VersionManifest getOrFetch() throws IOException {

public static VersionManifest fetch() throws IOException {
try (
InputStream in = new URL(MANIFEST_URL).openStream();
InputStream in = openInputStream(MANIFEST_URL);
Reader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))
) {
instance = GSON.fromJson(reader, VersionManifest.class);
Expand Down Expand Up @@ -118,7 +120,7 @@ public static class Version implements Comparable<Version> {
public synchronized VersionDetail fetchDetail() throws IOException {
if (detail == null) {
try (
InputStream in = new URL(url).openStream();
InputStream in = openInputStream(url);
Reader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))
) {
detail = GSON.fromJson(reader, VersionDetail.class);
Expand Down Expand Up @@ -153,6 +155,23 @@ public static class Download {
private String url;
private long size;
private String sha1;

public InputStream createInputStream() throws IOException {
return openInputStream(url);
}

}

private static InputStream openInputStream(String urlPath) throws IOException {
try {
URL downloadUrl = new URI(urlPath).toURL();
URLConnection connection = downloadUrl.openConnection();
connection.setConnectTimeout(CONNECTION_TIMEOUT);
connection.setReadTimeout(CONNECTION_TIMEOUT);
return connection.getInputStream();
} catch (URISyntaxException e) {
throw new IOException(e);
}
}

}

0 comments on commit b3dfc47

Please sign in to comment.