From f7e59a21966f76cbfc0b3b2d31989cfca9e7b692 Mon Sep 17 00:00:00 2001 From: Jason Penilla <11360596+jpenilla@users.noreply.github.com> Date: Wed, 5 Jul 2023 19:25:23 -0700 Subject: [PATCH] don't create redundant threads for DependencyDownloader http client --- .../draycia/carbon/common/util/DependencyDownloader.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/common/src/main/java/net/draycia/carbon/common/util/DependencyDownloader.java b/common/src/main/java/net/draycia/carbon/common/util/DependencyDownloader.java index dbf94b51..78e821f1 100644 --- a/common/src/main/java/net/draycia/carbon/common/util/DependencyDownloader.java +++ b/common/src/main/java/net/draycia/carbon/common/util/DependencyDownloader.java @@ -59,6 +59,7 @@ public final class DependencyDownloader { private final List relocations = new ArrayList<>(); private final Logger logger; private final Path cacheDir; + private final HttpClient client; public DependencyDownloader( final Logger logger, @@ -66,6 +67,10 @@ public final class DependencyDownloader { ) { this.logger = logger; this.cacheDir = cacheDir; + this.client = HttpClient.newBuilder() + // using a cached thread pool (default) is completely redundant with how we use the client + .executor(Runnable::run) + .build(); } public Set resolve() { @@ -140,7 +145,6 @@ public final class DependencyDownloader { } private Path resolve(final Dependency dependency, final Runnable attemptingDownloadCallback) throws IOException { - final HttpClient client = HttpClient.newHttpClient(); @Nullable Path resolved = null; final Path outputFile = this.cacheDir.resolve(String.format( "%s/%s/%s/%s-%s.jar", @@ -178,7 +182,7 @@ public final class DependencyDownloader { try { Files.createDirectories(outputFile.getParent()); //this.logger.info("attempting to download " + urlString); - response = client.send(request, HttpResponse.BodyHandlers.ofFile( + response = this.client.send(request, HttpResponse.BodyHandlers.ofFile( outputFile, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE,