Skip to content

Commit

Permalink
Retry on connection errors
Browse files Browse the repository at this point in the history
  • Loading branch information
psainics committed Apr 8, 2024
1 parent 3bc7a6e commit b7a2c31
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ private InputStream callEntityData(long top, String entityName)
addQueryParameter(TOP_OPTION, String.valueOf(top)).addQueryParameter(SELECT_OPTION, selectFields.toString())
.build().url();
SuccessFactorsTransporter successFactorsHttpClient = new SuccessFactorsTransporter(config);
SuccessFactorsResponseContainer responseContainer = successFactorsHttpClient.callSuccessFactorsWithRetry(dataURL);
SuccessFactorsResponseContainer responseContainer = successFactorsHttpClient
.callSuccessFactorsWithRetry(dataURL, MediaType.APPLICATION_JSON);

ExceptionParser.checkAndThrowException("", responseContainer);
return responseContainer.getResponseStream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,14 @@ private SuccessFactorsEntityProvider fetchServiceMetadata(InputStream metadataSt
* @throws TransportException any http client exceptions are wrapped under it.
*/
private InputStream callEntityMetadata() throws TransportException {
try {
SuccessFactorsResponseContainer responseContainer = successFactorsHttpClient
.callSuccessFactorsEntity(urlContainer.getMetadataURL(), MediaType.APPLICATION_XML, METADATA);
.callSuccessFactorsWithRetry(urlContainer.getMetadataURL(), MediaType.APPLICATION_XML);
return responseContainer.getResponseStream();
} catch (IOException e) {
LOG.error("Error occurred while fetching metadata for entity {}", pluginConfig.getEntityName());
throw new RuntimeException(e);
}
}

/**
Expand Down Expand Up @@ -320,7 +325,8 @@ private InputStream callEntityData(@Nullable Long skip, @Nullable Long top)
} else {
dataURL = urlContainer.getDataFetchURL(skip, top);
}
SuccessFactorsResponseContainer responseContainer = successFactorsHttpClient.callSuccessFactorsWithRetry(dataURL);
SuccessFactorsResponseContainer responseContainer = successFactorsHttpClient
.callSuccessFactorsWithRetry(dataURL, MediaType.APPLICATION_JSON);

ExceptionParser.checkAndThrowException("", responseContainer);
return responseContainer.getResponseStream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
Expand All @@ -59,7 +60,7 @@ public class SuccessFactorsTransporter {
private static final Logger LOG = LoggerFactory.getLogger(SuccessFactorsTransporter.class);
private static final long CONNECTION_TIMEOUT = 300;
private static final long WAIT_TIME = 5;
private static final long MAX_NUMBER_OF_RETRY_ATTEMPTS = 5;
private static final long MAX_NUMBER_OF_RETRY_ATTEMPTS = 10;

private SuccessFactorsConnectorConfig config;
private Response response;
Expand Down Expand Up @@ -103,10 +104,10 @@ public SuccessFactorsResponseContainer callSuccessFactorsEntity(URL endpoint, St
* @throws IOException any http client exceptions
* @throws TransportException any error while preparing the {@code OkHttpClient}
*/
public SuccessFactorsResponseContainer callSuccessFactorsWithRetry(URL endpoint)
public SuccessFactorsResponseContainer callSuccessFactorsWithRetry(URL endpoint, String mediaType)
throws IOException, TransportException {

Response res = retrySapTransportCall(endpoint, MediaType.APPLICATION_JSON);
Response res = retrySapTransportCall(endpoint, mediaType);

try {
return prepareResponseContainer(res);
Expand All @@ -126,16 +127,27 @@ public SuccessFactorsResponseContainer callSuccessFactorsWithRetry(URL endpoint)
*/
public Response retrySapTransportCall(URL endpoint, String mediaType) throws IOException {
Callable<Boolean> fetchRecords = () -> {
response = transport(endpoint, mediaType);
if (response != null && response.code() >= HttpURLConnection.HTTP_INTERNAL_ERROR) {
try {
LOG.debug("Retrying the call to URL {}.", endpoint);
response = transport(endpoint, mediaType);
if (response != null && response.code() >= HttpURLConnection.HTTP_INTERNAL_ERROR) {
throw new RetryableException();
}
} catch (ConnectException e) {
LOG.error("Retry Failed with ConnectException for URL {}.", endpoint);
LOG.error(e.toString());
throw new RetryableException();
} catch (IOException e) {
LOG.error("Retry Failed with IOException for URL {}.", endpoint);
LOG.error(e.toString());
throw new RetryableException();
}
return true;
};

Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder()
.retryIfExceptionOfType(RetryableException.class)
.withWaitStrategy(WaitStrategies.exponentialWait(WAIT_TIME, TimeUnit.SECONDS))
.withWaitStrategy(WaitStrategies.exponentialWait(100, 5 * 60, TimeUnit.SECONDS))
.withStopStrategy(StopStrategies.stopAfterAttempt((int) MAX_NUMBER_OF_RETRY_ATTEMPTS))
.build();

Expand Down

0 comments on commit b7a2c31

Please sign in to comment.