Retry gradle wrapper download on http 500 and 503. (#11766)

This commit is contained in:
Dawid Weiss 2022-09-13 10:30:20 +02:00 committed by GitHub
parent 30b72ec364
commit e491ef797c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 35 additions and 10 deletions

View File

@ -17,7 +17,11 @@
package org.apache.lucene.gradle;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
@ -29,6 +33,7 @@ import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.EnumSet;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import static java.nio.file.StandardOpenOption.APPEND;
@ -87,21 +92,38 @@ public class WrapperDownloader {
}
}
URL url = new URL("https://github.com/gradle/gradle/raw/v" + wrapperVersion + "/gradle/wrapper/gradle-wrapper.jar");
URL url = new URL("https://raw.githubusercontent.com/gradle/gradle/v" + wrapperVersion + "/gradle/wrapper/gradle-wrapper.jar");
System.err.println("Downloading gradle-wrapper.jar from " + url);
// As of v6.0.1 the wrapper is approximately 60K
// Can increase this if gradle wrapper ever goes beyond 500K, but keep a safety check
final int maxSize = 512 * 1024;
// Zero-copy save the jar to a temp file
Path temp = Files.createTempFile(destination.getParent(), ".gradle-wrapper", ".tmp");
try {
try (ReadableByteChannel in = Channels.newChannel(url.openStream());
FileChannel out = FileChannel.open(temp, EnumSet.of(APPEND))) {
out.transferFrom(in, 0, maxSize);
} catch (IOException e) {
throw new IOException("Could not download gradle-wrapper.jar (" + e.getMessage() + ").");
int retries = 3;
int retryDelay = 30;
HttpURLConnection connection;
while (true) {
connection = (HttpURLConnection) url.openConnection();
connection.connect();
switch (connection.getResponseCode()) {
case HttpURLConnection.HTTP_INTERNAL_ERROR:
case HttpURLConnection.HTTP_UNAVAILABLE:
case HttpURLConnection.HTTP_BAD_GATEWAY:
if (retries-- > 0) {
// Retry after a short delay.
System.err.println("Server returned HTTP " + connection.getResponseCode() + ", will retry in " + retryDelay + " seconds.");
Thread.sleep(TimeUnit.SECONDS.toMillis(retryDelay));
continue;
}
}
// fall through, let getInputStream() throw IOException if there's a failure.
break;
}
try (InputStream is = connection.getInputStream();
OutputStream out = Files.newOutputStream(temp)){
is.transferTo(out);
}
String checksum = checksum(digest, temp);
@ -114,6 +136,9 @@ public class WrapperDownloader {
Files.move(temp, destination, REPLACE_EXISTING);
temp = null;
} catch (IOException | InterruptedException e) {
throw new IOException("Could not download gradle-wrapper.jar (" +
e.getClass().getSimpleName() + ": " + e.getMessage() + ").");
} finally {
if (temp != null) {
Files.deleteIfExists(temp);