Skip to content

Commit

Permalink
chore: Add work-around for 404 errors on newly created resources
Browse files Browse the repository at this point in the history
  • Loading branch information
rsenden committed Sep 25, 2023
1 parent f397d8c commit 7e84ab2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -57,6 +57,6 @@ private ApacheClient createClient(Config config) {
}

private void configureClient(HttpClientBuilder cb) {
cb.setServiceUnavailableRetryStrategy(new FoDRateLimitRetryStrategy());
cb.setServiceUnavailableRetryStrategy(new FoDRetryStrategy());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Long> interval = new ThreadLocal<Long>();

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;
}

Expand Down

0 comments on commit 7e84ab2

Please sign in to comment.