Skip to content

Commit

Permalink
Increase retry number to 5 and randomize wait time a little
Browse files Browse the repository at this point in the history
Still sometimes 3 retries are not enough e.g. for Maven Central
  • Loading branch information
mvilliger committed Jan 19, 2023
1 parent 12b1850 commit 081f412
Showing 1 changed file with 9 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public static InputStream httpGet(URI uri) throws IOException {

static InputStream doHttpGetWithRetry(URI uri) throws IOException, InterruptedException {
IOException exception = null;
var numRetries = 3; // retry 3 times as some resources might be temporary unavailable
var numRetries = 5; // retry 5 times as some resources might be temporary unavailable
for (var attempt = 1; attempt <= numRetries; attempt++) {
try {
return doHttpGet(uri);
Expand All @@ -181,12 +181,19 @@ static InputStream doHttpGetWithRetry(URI uri) throws IOException, InterruptedEx
exception = e;
}
if (attempt < numRetries) {
Thread.sleep(attempt * 500L); // quick wait between retries (500ms, 1s)
Thread.sleep(getSleepTimeAfterAttempt(attempt)); // quick wait between retries
}
}
throw exception;
}

@SuppressWarnings({"UnsecureRandomNumberGeneration", "NumericCastThatLosesPrecision"})
static long getSleepTimeAfterAttempt(int attempt) {
var baseTimeout = attempt * 300L;
var randomOffset = (long) (Math.random() * 200);
return baseTimeout + randomOffset;
}

static InputStream doHttpGet(URI uri) throws IOException, InterruptedException {
var timeout = Duration.ofSeconds(10);
var request = HttpRequest.newBuilder()
Expand Down

0 comments on commit 081f412

Please sign in to comment.