Skip to content

Commit

Permalink
fix retry middleware to capture ApiHttpExceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
jenschude committed Nov 4, 2020
1 parent 90f3c0e commit d597dda
Showing 1 changed file with 9 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package io.vrap.rmf.base.client.http;

import io.vrap.rmf.base.client.ApiHttpException;
import io.vrap.rmf.base.client.ApiHttpRequest;
import io.vrap.rmf.base.client.ApiHttpResponse;
import io.vrap.rmf.base.client.ClientFactory;
import net.jodah.failsafe.Failsafe;
import net.jodah.failsafe.FailsafeExecutor;
import net.jodah.failsafe.RetryPolicy;
Expand All @@ -18,7 +20,7 @@
import static java.util.Collections.singletonList;

public class RetryMiddleware implements Middleware, AutoCloseable {
Logger logger = LoggerFactory.getLogger("commercetools");
Logger logger = LoggerFactory.getLogger(ClientFactory.COMMERCETOOLS);

private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
private final FailsafeExecutor<ApiHttpResponse<byte[]>> failsafeExecutor;
Expand All @@ -37,7 +39,12 @@ public RetryMiddleware(final int maxRetries, final long delay, final long maxDel

public RetryMiddleware(final int maxRetries, final long delay, final long maxDelay, List<Integer> statusCodes) {
RetryPolicy<ApiHttpResponse<byte[]>> retryPolicy = new RetryPolicy<ApiHttpResponse<byte[]>>()
.handleResultIf(o -> statusCodes.contains(o.getStatusCode()))
.handleIf((response, throwable) -> {
if (throwable instanceof ApiHttpException) {
return statusCodes.contains(((ApiHttpException)throwable).getStatusCode());
}
return statusCodes.contains(response.getStatusCode());
})
.withBackoff(delay, maxDelay, ChronoUnit.MILLIS)
.onRetry(event -> logger.info("Retry #" + event.getAttemptCount()))
.withMaxRetries(maxRetries);
Expand Down

0 comments on commit d597dda

Please sign in to comment.