if (isClosed() || (current().getNextUri() == null)) {
valid.set(false);
return false;
}
Request request = prepareGet()
.setHeader(USER_AGENT, USER_AGENT_VALUE)
.setUri(current().getNextUri())
.build();
Exception cause = null;
long start = System.nanoTime();
long attempts = 0;
do {
// back-off on retry
if (attempts > 0) {
sleepUninterruptibly(attempts * 100, MILLISECONDS);
}
attempts++;
JsonResponse<QueryResults> response;
try {
response = httpClient.execute(request, responseHandler);
}
catch (RuntimeException e) {
cause = e;
continue;
}
if (response.getStatusCode() == HttpStatus.OK.code() && response.hasValue()) {
currentResults.set(response.getValue());
return true;
}
if (response.getStatusCode() != HttpStatus.SERVICE_UNAVAILABLE.code()) {
gone.set(true);
if (!response.hasValue()) {
throw new RuntimeException(format("Error fetching next at %s returned an invalid response", request.getUri()),
response.getException());
}
throw new RuntimeException(format("Error fetching next at %s returned %s: %s",
request.getUri(),
response.getStatusCode(),
response.getStatusMessage()));
}
}
while ((System.nanoTime() - start) < MINUTES.toNanos(2) && !isClosed());