Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: additional logging #162

Merged
merged 3 commits into from
May 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading