Skip to content

Commit

Permalink
Merge pull request #1832 from scireum/feature/fha/SE-12943_VirtualFil…
Browse files Browse the repository at this point in the history
…e_Retries

SE-12943: Allows Configuring a Custom Number of retries for 503 Responses
  • Loading branch information
fhaScireum authored Sep 7, 2023
2 parents f2d9b4a + 0d4933d commit a27b803
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/main/java/sirius/biz/storage/layer3/VirtualFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import sirius.biz.storage.layer2.Blob;
import sirius.biz.storage.util.Attempt;
import sirius.biz.storage.util.StorageUtils;
import sirius.kernel.Sirius;
import sirius.kernel.async.TaskContext;
import sirius.kernel.commons.Files;
import sirius.kernel.commons.Streams;
Expand Down Expand Up @@ -82,7 +83,6 @@ public abstract class VirtualFile extends Composable implements Comparable<Virtu
private static final String HANDLER_CONSUME_FILE_HANDLER = "consumeFileHandler";
private static final String MESSAGE_KEY_LOAD_FROM_URL_FAILED = "$VirtualFile.loadFromUrlFailed";
private static final String MESSAGE_KEY_LOAD_FROM_URL_DISABLED = "$VirtualFile.loadFromUrlDisabled";
private static final int NUMBER_OF_RETRIES_FOR_SERVICE_UNAVAILABLE = 3;

protected String name;
protected String description;
Expand Down Expand Up @@ -122,6 +122,9 @@ public abstract class VirtualFile extends Composable implements Comparable<Virtu
@PriorityParts(RemoteFileResolver.class)
private static List<RemoteFileResolver> remoteFileResolvers;

@ConfigValue("storage.layer3.retriesForServiceUnavailable")
private static int retriesForServiceUnavailable;

/**
* Internal constructor to create the "/" directory.
*/
Expand Down Expand Up @@ -1390,8 +1393,7 @@ public boolean performLoadFromUri(URI uri, FetchFromUrlMode mode) {
return false;
}

HttpResponse<InputStream> response =
requestFileFromUri(uri, mode, NUMBER_OF_RETRIES_FOR_SERVICE_UNAVAILABLE);
HttpResponse<InputStream> response = requestFileFromUri(uri, mode, retriesForServiceUnavailable);

if (response.statusCode() == HttpResponseStatus.NOT_MODIFIED.code()) {
tryTouch();
Expand Down Expand Up @@ -1431,14 +1433,14 @@ private HttpResponse<InputStream> requestFileFromUri(URI uri, FetchFromUrlMode m

if (retries > 0) {
// Wait 200ms, 700ms, 1200ms...
Wait.millis(200 + (NUMBER_OF_RETRIES_FOR_SERVICE_UNAVAILABLE - retries) * 500);
Wait.millis(200 + (retriesForServiceUnavailable - retries) * 500);
return requestFileFromUri(uri, mode, retries - 1);
}

throw new IOException(Strings.apply("The server responded with status %s (%s) after %s retries!",
HttpResponseStatus.valueOf(response.statusCode()).toString(),
response.statusCode(),
NUMBER_OF_RETRIES_FOR_SERVICE_UNAVAILABLE));
retriesForServiceUnavailable));
}

if (response.statusCode() >= 400) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/component-biz.conf
Original file line number Diff line number Diff line change
Expand Up @@ -1367,6 +1367,10 @@ storage {
# to determine if the file-extension of a file-url contains the effective filename or if the path
# is just a script and we need to inspect the ContentDisposition (etc.) to obtain the actual file name
serverSidedScriptingExtensions = [ "php", "php3", "php4", "php5", "cgi", "asp", "aspx", "jsp", "jspx", "cfm", "cfml", "xml" ]

# Defines the number of retries for service unavailable responses (HTTP 503).
# The first retry will wait 200ms with each consequent retry waiting 500ms longer than the previous one.
retriesForServiceUnavailable = 3
}

}
Expand Down

0 comments on commit a27b803

Please sign in to comment.