@Test public void shouldCreateNewRetryableExceptionWithMatchingNonnullRetryAfterWhen503AndNotRetryableException() { Response response = makeDefaultDecoderReplyWithHeadersWhenReceivingResponse( STATUS_503, NON_RETRYABLE_EXCEPTION, HEADERS_WITH_RETRY_AFTER); Exception exception = atlasDbDecoder.decode(EMPTY_METHOD_KEY, response); assertThat(((RetryableException) exception).retryAfter(), is(RETRY_AFTER_DATE)); }
@Test public void shouldCreateNewRetryableExceptionWithMatchingNullRetryAfterWhen503AndNotRetryableException() { Response response = makeDefaultDecoderReplyWhenReceivingResponse(STATUS_503, NON_RETRYABLE_EXCEPTION); Exception exception = atlasDbDecoder.decode(EMPTY_METHOD_KEY, response); assertNull(((RetryableException) exception).retryAfter()); }
@Test public void shouldCreateNewRetryableExceptionWithNullRetryAfterWhen429WithNullRetryAfterAndNotRetryableException() { Response response = makeDefaultDecoderReplyWhenReceivingResponse(STATUS_429, NON_RETRYABLE_EXCEPTION); Exception exception = atlasDbDecoder.decode(EMPTY_METHOD_KEY, response); assertNull(((RetryableException) exception).retryAfter()); }
@Test public void shouldCreateNewRetryableExceptionWithNullRetryAfterWhen429WithRetryAfterAndNotRetryableException() { Response response = makeDefaultDecoderReplyWithHeadersWhenReceivingResponse( STATUS_429, NON_RETRYABLE_EXCEPTION, HEADERS_WITH_RETRY_AFTER); Exception exception = atlasDbDecoder.decode(EMPTY_METHOD_KEY, response); assertNull(((RetryableException) exception).retryAfter()); }
public static ReactiveRetryPolicy retryWithDelay( int maxRetries, long period) { return (error, attemptNo) -> { if (attemptNo <= maxRetries) { long delay; Date retryAfter; // "Retry-After" header set if (error instanceof RetryableException && (retryAfter = ((RetryableException) error) .retryAfter()) != null) { delay = retryAfter.getTime() - System.currentTimeMillis(); delay = Math.min(delay, period); delay = Math.max(delay, 0); } else { delay = period; } return delay; } else { return -1; } }; }
public static ReactiveRetryPolicy retryWithBackoff(int maxRetries, long periodInMs) { return (error, attemptNo) -> { if (attemptNo <= maxRetries) { long delay; Date retryAfter; // "Retry-After" header set if (error instanceof RetryableException && (retryAfter = ((RetryableException) error) .retryAfter()) != null) { delay = retryAfter.getTime() - System.currentTimeMillis(); delay = Math.min(delay, periodInMs); delay = Math.max(delay, 0); } else { delay = periodInMs; } return delay; } else { return -1; } }; }
public void continueOrPropagate(RetryableException e) { if (attempt++ >= maxAttempts) { throw e; } long interval; if (e.retryAfter() != null) { interval = e.retryAfter().getTime() - currentTimeMillis(); if (interval > maxPeriod) { interval = maxPeriod; } if (interval < 0) { return; } } else { interval = nextMaxInterval(); } try { Thread.sleep(interval); } catch (InterruptedException ignored) { Thread.currentThread().interrupt(); } sleptForMillis += interval; }