Skip to content

Commit 602dbfc

Browse files
committed
Add attemptNo & delayMs to RetryOn interface, re #55
1 parent 71fccdc commit 602dbfc

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

src/main/java/org/libj/util/retry/RetryOn.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ public interface RetryOn {
2525
* Specifies the conditions under which a retry should occur given the provided {@link Exception}.
2626
*
2727
* @param e The non-null exception that occurred during execution of a {@link Retryable} object.
28+
* @param attemptNo The attempt number on which the exception was thrown.
29+
* @param delayMs The delay (in milliseconds) from the previous invocation attempt.
2830
* @return {@code true} if a retry should occur, otherwise {@code false}.
2931
*/
30-
boolean retryOn(Exception e);
32+
boolean retryOn(Exception e, int attemptNo, long delayMs);
3133
}

src/main/java/org/libj/util/retry/RetryPolicy.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ private final <T> T run0(final Retryable<T,E> retryable, final long timeout) thr
438438
final long startTime = System.currentTimeMillis();
439439
long runTime = 0;
440440
Exception previousException = null;
441+
long delayMs = 0;
441442
for (int attemptNo = 1;; ++attemptNo) { // [N]
442443
if (onRetry != null)
443444
onRetry.accept(attemptNo);
@@ -449,10 +450,10 @@ private final <T> T run0(final Retryable<T,E> retryable, final long timeout) thr
449450
if (previousException == null || e.getClass() != previousException.getClass() || !Objects.equals(e.getMessage(), previousException.getMessage()))
450451
exceptions.add(previousException = e);
451452

452-
if (attemptNo > maxRetries || !retryOn.retryOn(e))
453-
retryFailed(exceptions, attemptNo, getDelayMs(attemptNo - 1));
453+
if (attemptNo > maxRetries || !retryOn.retryOn(e, attemptNo, delayMs))
454+
retryFailed(exceptions, attemptNo, delayMs);
454455

455-
long delayMs = getDelayMs(attemptNo);
456+
delayMs = getDelayMs(attemptNo);
456457
if (jitter > 0)
457458
delayMs *= jitter * Math.random() + 1;
458459

src/test/java/org/libj/util/retry/RetryPolicyTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class RetryPolicyTest {
2828
@Test
2929
public void testNonNullOnRetryFailure() throws RetryFailureRuntimeException, Exception {
3030
try {
31-
new RetryPolicy<>(e -> true, null, (e, se, a, d) -> null, 100, 100).run((p, a) -> null);
31+
new RetryPolicy<>((e, a, d) -> true, null, (e, se, a, d) -> null, 100, 100).run((p, a) -> null);
3232
fail("Expected NullPointerException");
3333
}
3434
catch (final NullPointerException e) {
@@ -50,7 +50,7 @@ public void testLinearBackoff() {
5050
for (int i = 0; i < attempts - 1; ++i) // [N]
5151
timings[i + 1] = timings[i] + delays[i];
5252

53-
assertEquals("PASS", new RetryPolicy<>(e -> true, null, (e, se, a, d) -> new RuntimeException(), attempts, delayMs).run((p, a) -> {
53+
assertEquals("PASS", new RetryPolicy<>((e, a, d) -> true, null, (e, se, a, d) -> new RuntimeException(), attempts, delayMs).run((p, a) -> {
5454
if (index[0] < attempts) {
5555
final long delayMs1 = p.getDelayMs(a);
5656
assertEquals(delays[index[0]++], delayMs1);
@@ -83,7 +83,7 @@ public void testExponentialBackoff() throws RetryFailureException {
8383
for (int i = 0; i < attempts - 1; ++i) // [N]
8484
timings[i + 1] = timings[i] + delays[i];
8585

86-
assertEquals("PASS", new RetryPolicy<>(e -> true, null, (e, se, a, d) -> new RetryFailureException(a, d), attempts, startDelay, 0, false, factor, maxDelay).run((p, a) -> {
86+
assertEquals("PASS", new RetryPolicy<>((e, a, d) -> true, null, (e, se, a, d) -> new RetryFailureException(a, d), attempts, startDelay, 0, false, factor, maxDelay).run((p, a) -> {
8787
if (index[0] < attempts) {
8888
final long delayMs = p.getDelayMs(a);
8989
assertEquals(delays[index[0]++], delayMs);

0 commit comments

Comments
 (0)