Skip to content

Commit

Permalink
fix: additional logging (#162)
Browse files Browse the repository at this point in the history
* fix: Adding a call to shutdown() to avoid the consequences of Executor errors

* fix: Adding additional DEBUG Level logs in NvdApiRetryStrategy.
  • Loading branch information
SVBryakin authored May 27, 2024
1 parent 7a09158 commit 98959d2
Showing 1 changed file with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.time.Instant;
import java.util.ArrayList;
Expand All @@ -53,33 +54,45 @@ public class NvdApiRetryStrategy extends DefaultHttpRequestRetryStrategy {
*/
private final long delay;

private static final String RETRY_MESSAGE_TEMPLATE = "NVD API request failures are occurring; retrying request for the {} time";

public NvdApiRetryStrategy(int maxRetries, long delay) {
super(maxRetries, TimeValue.of(delay, TimeUnit.MILLISECONDS), new ArrayList<Class<? extends IOException>>(),
super(maxRetries, TimeValue.of(delay, TimeUnit.MILLISECONDS), new ArrayList<>(),
Arrays.asList(429, 502, 503, 504));
this.maxRetries = maxRetries;
this.delay = delay;
}

@Override
public boolean retryRequest(HttpRequest request, IOException exception, int execCount, HttpContext context) {
if (execCount >= (maxRetries / 2)) {
LOG.warn("NVD API request failures are occurring; retrying request for the {} time", execCount);
} else if (execCount > 1) {
LOG.debug("Retrying request {} : {} time", request.getRequestUri(), execCount);
}
public boolean retryRequest(@Nonnull HttpRequest request, @Nonnull IOException exception, int execCount, HttpContext context) {
logRetryState(request, exception, execCount);
return super.retryRequest(request, exception, execCount, context);
}

@Override
public boolean retryRequest(HttpResponse response, int execCount, HttpContext context) {
if (execCount >= (maxRetries / 2)) {
LOG.warn("NVD API request failures are occurring; retrying request for the {} time", execCount);
LOG.warn(RETRY_MESSAGE_TEMPLATE, execCount);
} else if (execCount > 1) {
LOG.debug("Retrying request {} time", execCount);
}
return super.retryRequest(response, execCount, context);
}

private void logRetryState(@Nonnull HttpRequest request, @Nonnull IOException exception, int execCount) {
if (execCount >= (maxRetries / 2)) {
LOG.warn(RETRY_MESSAGE_TEMPLATE, execCount);
if (LOG.isDebugEnabled()) {
LOG.debug("NVD API request failures with exception : {}. Message: {}", exception.getClass().getName(), exception.getMessage());
}
} else if (execCount > 1) {
LOG.warn("Retrying request {} : {} time", request.getRequestUri(), execCount);
if (LOG.isDebugEnabled()) {
LOG.debug("Retrying request with exception {}. Message: {}", exception.getClass().getName(), exception.getMessage());
}
}
}

@Override
public TimeValue getRetryInterval(final HttpResponse response, final int execCount, final HttpContext context) {
TimeValue value;
Expand Down

0 comments on commit 98959d2

Please sign in to comment.