Skip to content

Commit

Permalink
#31: Add repeater on 5xx http error codes.
Browse files Browse the repository at this point in the history
  • Loading branch information
heshamMassoud committed Jul 21, 2017
1 parent f3b38fd commit ba08c6a
Showing 1 changed file with 23 additions and 1 deletion.
Expand Up @@ -2,20 +2,31 @@

import io.sphere.sdk.client.BlockingSphereClient;
import io.sphere.sdk.client.QueueSphereClientDecorator;
import io.sphere.sdk.client.RetrySphereClientDecorator;
import io.sphere.sdk.client.SphereAccessTokenSupplier;
import io.sphere.sdk.client.SphereClient;
import io.sphere.sdk.client.SphereClientConfig;
import io.sphere.sdk.http.AsyncHttpClientAdapter;
import io.sphere.sdk.http.HttpClient;
import io.sphere.sdk.retry.RetryAction;
import io.sphere.sdk.retry.RetryPredicate;
import io.sphere.sdk.retry.RetryRule;
import org.asynchttpclient.AsyncHttpClient;
import org.asynchttpclient.DefaultAsyncHttpClient;
import org.asynchttpclient.DefaultAsyncHttpClientConfig;

import javax.annotation.Nonnull;
import java.time.Duration;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static io.sphere.sdk.http.HttpStatusCode.BAD_GATEWAY_502;
import static io.sphere.sdk.http.HttpStatusCode.GATEWAY_TIMEOUT_504;
import static io.sphere.sdk.http.HttpStatusCode.SERVICE_UNAVAILABLE_503;

public class ClientConfigurationUtils {
private static HttpClient httpClient;
private static final long DEFAULT_TIMEOUT = 30000;
Expand All @@ -36,7 +47,8 @@ public static synchronized SphereClient createClient(@Nonnull final SphereClient
SphereAccessTokenSupplier.ofAutoRefresh(clientConfig, httpClient, false);
final SphereClient underlying = SphereClient.of(clientConfig, httpClient, tokenSupplier);
final SphereClient limitedParallelRequestsClient = withLimitedParallelRequests(underlying);
delegatesCache.put(clientConfig, limitedParallelRequestsClient);
final SphereClient retryClient = withRetry(limitedParallelRequestsClient);
delegatesCache.put(clientConfig, retryClient);
}
return BlockingSphereClient.of(delegatesCache.get(clientConfig), timeout, timeUnit);
}
Expand Down Expand Up @@ -68,6 +80,16 @@ private static synchronized HttpClient getHttpClient() {
return httpClient;
}

private static SphereClient withRetry(final SphereClient delegate) {
final int maxAttempts = 5;
final RetryAction scheduledRetry = RetryAction
.ofScheduledRetry(maxAttempts, context -> Duration.ofSeconds(context.getAttempt() * 2));
final RetryPredicate http5xxMatcher = RetryPredicate
.ofMatchingStatusCodes(BAD_GATEWAY_502, SERVICE_UNAVAILABLE_503, GATEWAY_TIMEOUT_504);
final List<RetryRule> retryRules = Collections.singletonList(RetryRule.of(http5xxMatcher, scheduledRetry));
return RetrySphereClientDecorator.of(delegate, retryRules);
}

private static SphereClient withLimitedParallelRequests(final SphereClient delegate) {
final int maxParallelRequests = 20;
return QueueSphereClientDecorator.of(delegate, maxParallelRequests);
Expand Down

0 comments on commit ba08c6a

Please sign in to comment.