@VisibleForTesting
boolean handleConnectionResult(@Nullable StatusLine statusLine) {
statsReporter.incrNumConnects();
if (statusLine == null) {
logger.warn("{} failed to establish connection properly", name);
addEvent(new Event(EventType.CONNECTION_ERROR, "Failed to establish connection properly"));
return false;
}
int statusCode = statusLine.getStatusCode();
if (statusCode == HttpConstants.Codes.SUCCESS) {
logger.debug("{} Connection successfully established", name);
statsReporter.incrNum200s();
connectionEstablished.set(true);
addEvent(new HttpResponseEvent(EventType.CONNECTED, statusLine));
reconnectionManager.resetCounts();
return true;
}
logger.warn(name + " Error connecting w/ status code - {}, reason - {}", statusCode, statusLine.getReasonPhrase());
statsReporter.incrNumConnectionFailures();
addEvent(new HttpResponseEvent(EventType.HTTP_ERROR, statusLine));
if (HttpConstants.FATAL_CODES.contains(statusCode)) {
setExitStatus(new Event(EventType.STOPPED_BY_ERROR, "Fatal error code: " + statusCode));
} else if (statusCode < 500 && statusCode >= 400) {
statsReporter.incrNum400s();
// we will retry these a set number of times, then fail
if (reconnectionManager.shouldReconnectOn400s()) {
logger.debug("{} Reconnecting on {}", name, statusCode);
reconnectionManager.handleExponentialBackoff();
} else {
logger.debug("{} Reconnecting retries exhausted for {}", name, statusCode);
setExitStatus(new Event(EventType.STOPPED_BY_ERROR, "Retries exhausted"));
}
} else if (statusCode >= 500) {
statsReporter.incrNum500s();
reconnectionManager.handleExponentialBackoff();
} else {
setExitStatus(new Event(EventType.STOPPED_BY_ERROR, statusLine.getReasonPhrase()));
}
return false;
}