From 7e84ab23b12ba5b37f3e9a08c41dfd3138d0b055 Mon Sep 17 00:00:00 2001 From: Ruud Senden <8635138+rsenden@users.noreply.github.com> Date: Mon, 25 Sep 2023 16:51:47 +0200 Subject: [PATCH] chore: Add work-around for 404 errors on newly created resources --- .../mixin/FoDProductHelperBasicMixin.java | 4 +-- ...tryStrategy.java => FoDRetryStrategy.java} | 27 ++++++++++++------- 2 files changed, 19 insertions(+), 12 deletions(-) rename fcli-core/fcli-fod/src/main/java/com/fortify/cli/fod/_common/rest/helper/{FoDRateLimitRetryStrategy.java => FoDRetryStrategy.java} (70%) diff --git a/fcli-core/fcli-fod/src/main/java/com/fortify/cli/fod/_common/output/mixin/FoDProductHelperBasicMixin.java b/fcli-core/fcli-fod/src/main/java/com/fortify/cli/fod/_common/output/mixin/FoDProductHelperBasicMixin.java index 22b600173a..9aa2b8173c 100644 --- a/fcli-core/fcli-fod/src/main/java/com/fortify/cli/fod/_common/output/mixin/FoDProductHelperBasicMixin.java +++ b/fcli-core/fcli-fod/src/main/java/com/fortify/cli/fod/_common/output/mixin/FoDProductHelperBasicMixin.java @@ -20,7 +20,7 @@ import com.fortify.cli.common.rest.unirest.config.UnirestUnexpectedHttpResponseConfigurer; import com.fortify.cli.common.rest.unirest.config.UnirestUrlConfigConfigurer; import com.fortify.cli.common.session.cli.mixin.AbstractSessionUnirestInstanceSupplierMixin; -import com.fortify.cli.fod._common.rest.helper.FoDRateLimitRetryStrategy; +import com.fortify.cli.fod._common.rest.helper.FoDRetryStrategy; import com.fortify.cli.fod._common.session.helper.FoDSessionDescriptor; import com.fortify.cli.fod._common.session.helper.FoDSessionHelper; @@ -57,6 +57,6 @@ private ApacheClient createClient(Config config) { } private void configureClient(HttpClientBuilder cb) { - cb.setServiceUnavailableRetryStrategy(new FoDRateLimitRetryStrategy()); + cb.setServiceUnavailableRetryStrategy(new FoDRetryStrategy()); } } \ No newline at end of file diff --git a/fcli-core/fcli-fod/src/main/java/com/fortify/cli/fod/_common/rest/helper/FoDRateLimitRetryStrategy.java b/fcli-core/fcli-fod/src/main/java/com/fortify/cli/fod/_common/rest/helper/FoDRetryStrategy.java similarity index 70% rename from fcli-core/fcli-fod/src/main/java/com/fortify/cli/fod/_common/rest/helper/FoDRateLimitRetryStrategy.java rename to fcli-core/fcli-fod/src/main/java/com/fortify/cli/fod/_common/rest/helper/FoDRetryStrategy.java index 2e4f5bf8c1..1f543d55c1 100644 --- a/fcli-core/fcli-fod/src/main/java/com/fortify/cli/fod/_common/rest/helper/FoDRateLimitRetryStrategy.java +++ b/fcli-core/fcli-fod/src/main/java/com/fortify/cli/fod/_common/rest/helper/FoDRetryStrategy.java @@ -35,24 +35,31 @@ * that will retry a request if the server responds with an HTTP 429 (TOO_MANY_REQUESTS) * response. */ -public final class FoDRateLimitRetryStrategy implements ServiceUnavailableRetryStrategy { - private static final Log LOG = LogFactory.getLog(FoDRateLimitRetryStrategy.class); +public final class FoDRetryStrategy implements ServiceUnavailableRetryStrategy { + private static final Log LOG = LogFactory.getLog(FoDRetryStrategy.class); private final String HEADER_NAME = "X-Rate-Limit-Reset"; private int maxRetries = 2; private final ThreadLocal interval = new ThreadLocal(); - public FoDRateLimitRetryStrategy maxRetries(int maxRetries) { + public FoDRetryStrategy maxRetries(int maxRetries) { this.maxRetries = maxRetries; return this; } - public boolean retryRequest(HttpResponse response, int executionCount, HttpContext context) { - if ( executionCount < maxRetries+1 && response.getStatusLine().getStatusCode()==429 ) { - int retrySeconds = Integer.parseInt(response.getFirstHeader(HEADER_NAME).getValue()); - LOG.debug("Rate-limited request will be retried after "+retrySeconds+" seconds"); - interval.set((long)retrySeconds*1000); - return true; - } + public boolean retryRequest(HttpResponse response, int executionCount, HttpContext context) { + if ( executionCount < maxRetries+1 ) { + if ( response.getStatusLine().getStatusCode()==404 ) { + // Sometimes it can take a bit of time for FoD to properly register a scan request and + // possibly other newly created resources, hence we also retry on 404 errors. + interval.set((long)5000); + return true; + } else if ( response.getStatusLine().getStatusCode()==429 ) { + int retrySeconds = Integer.parseInt(response.getFirstHeader(HEADER_NAME).getValue()); + LOG.debug("Rate-limited request will be retried after "+retrySeconds+" seconds"); + interval.set((long)retrySeconds*1000); + return true; + } + } return false; }