|
| 1 | +package com.contentstack.sdk; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.logging.Logger; |
| 6 | + |
| 7 | +import okhttp3.Interceptor; |
| 8 | +import okhttp3.Request; |
| 9 | +import okhttp3.Response; |
| 10 | + |
| 11 | +public class RetryInterceptor implements Interceptor { |
| 12 | + |
| 13 | + private static final Logger logger = Logger.getLogger(RetryInterceptor.class.getName()); |
| 14 | + private final RetryOptions retryOptions; |
| 15 | + |
| 16 | + |
| 17 | + public RetryInterceptor(RetryOptions retryOptions) { |
| 18 | + if (retryOptions == null) { |
| 19 | + throw new NullPointerException("RetryOptions cannot be null"); |
| 20 | + } |
| 21 | + this.retryOptions = retryOptions; |
| 22 | + } |
| 23 | + |
| 24 | + @Override |
| 25 | + public Response intercept(Chain chain) throws IOException { |
| 26 | + Request request = chain.request(); |
| 27 | + Response response = null; |
| 28 | + IOException lastException = null; |
| 29 | + |
| 30 | + // If retry is disabled, just proceed with the request once |
| 31 | + if (!retryOptions.isRetryEnabled()) { |
| 32 | + return chain.proceed(request); |
| 33 | + } |
| 34 | + |
| 35 | + int attempt = 0; |
| 36 | + // retryLimit means number of retries, so total attempts = 1 initial + retryLimit retries |
| 37 | + int maxAttempts = retryOptions.getRetryLimit() + 1; |
| 38 | + |
| 39 | + while (attempt < maxAttempts) { |
| 40 | + |
| 41 | + try { |
| 42 | + if(response != null) { |
| 43 | + response.close(); |
| 44 | + } |
| 45 | + response = chain.proceed(request); |
| 46 | + |
| 47 | + if (shouldRetry(response.code()) && (attempt + 1) < maxAttempts) { |
| 48 | + logger.fine("Retry attempt " + (attempt + 1) + " for status " + response.code() + " on " + request.url()); |
| 49 | + |
| 50 | + long delay = calculateDelay(attempt, response.code(), null); |
| 51 | + Thread.sleep(delay); |
| 52 | + attempt++; |
| 53 | + continue; |
| 54 | + } |
| 55 | + return response; |
| 56 | + |
| 57 | + } catch (IOException e) { |
| 58 | + // Network error occurred |
| 59 | + lastException = e; |
| 60 | + |
| 61 | + if ((attempt + 1) < maxAttempts) { |
| 62 | + try { |
| 63 | + long delay = calculateDelay(attempt, -1, e); |
| 64 | + Thread.sleep(delay); |
| 65 | + attempt++; |
| 66 | + } catch (InterruptedException ie) { |
| 67 | + Thread.currentThread().interrupt(); |
| 68 | + throw new IOException("Retry interrupted", ie); |
| 69 | + } |
| 70 | + continue; |
| 71 | + } else { |
| 72 | + // No more retries, throw the exception |
| 73 | + throw e; |
| 74 | + } |
| 75 | + |
| 76 | + } catch (InterruptedException e) { |
| 77 | + // Thread was interrupted during sleep |
| 78 | + Thread.currentThread().interrupt(); |
| 79 | + if (response != null) response.close(); |
| 80 | + throw new IOException("Retry interrupted", e); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // Should not reach here normally |
| 85 | + if (lastException != null) { |
| 86 | + throw lastException; |
| 87 | + } |
| 88 | + return response; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Determines if a status code should trigger a retry. |
| 93 | + * |
| 94 | + * @param statusCode HTTP status code |
| 95 | + * @return true if this status code is retryable |
| 96 | + */ |
| 97 | + private boolean shouldRetry(int statusCode) { |
| 98 | + return Arrays.stream(retryOptions.getRetryableStatusCodes()).anyMatch(code -> code == statusCode); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Calculates the delay before the next retry attempt. |
| 103 | + * |
| 104 | + * @param attempt current attempt number (0-based) |
| 105 | + * @param statusCode HTTP status code (-1 for network errors) |
| 106 | + * @param exception the IOException that occurred (may be null) |
| 107 | + * @return delay in milliseconds |
| 108 | + */ |
| 109 | + private long calculateDelay(int attempt, int statusCode, IOException exception) { |
| 110 | + |
| 111 | + if(retryOptions.hasCustomBackoff()) { |
| 112 | + return retryOptions.getCustomBackoffStrategy().calculateDelay(attempt, statusCode, exception); |
| 113 | + } |
| 114 | + long baseDelay = retryOptions.getRetryDelay(); |
| 115 | + |
| 116 | + switch (retryOptions.getBackoffStrategy()) { |
| 117 | + case FIXED: |
| 118 | + // baseDelay already set above |
| 119 | + break; |
| 120 | + |
| 121 | + case LINEAR: |
| 122 | + baseDelay = retryOptions.getRetryDelay() * (attempt + 1); |
| 123 | + break; |
| 124 | + |
| 125 | + case EXPONENTIAL: |
| 126 | + baseDelay = (long) (retryOptions.getRetryDelay() * Math.pow(2,attempt)); |
| 127 | + break; |
| 128 | + |
| 129 | + default: |
| 130 | + // baseDelay already set above |
| 131 | + break; |
| 132 | + } |
| 133 | + |
| 134 | + return baseDelay; |
| 135 | + } |
| 136 | + |
| 137 | +} |
0 commit comments