Skip to content

Commit

Permalink
Dynamic -> compute delays
Browse files Browse the repository at this point in the history
  • Loading branch information
jhalterman committed Apr 6, 2018
1 parent 5457dde commit ba34f8c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
18 changes: 9 additions & 9 deletions src/main/java/net/jodah/failsafe/AbstractExecution.java
Expand Up @@ -33,7 +33,7 @@ abstract class AbstractExecution extends ExecutionContext {
volatile boolean completed;
volatile boolean retriesExceeded;
volatile boolean success;
/** The fixed, backoff, random or dynamic delay time in nanoseconds. */
/** The fixed, backoff, random or computed delay time in nanoseconds. */
private volatile long delayNanos = -1;
/** The wait time, which is the delay time adjusted for jitter and max duration, in nanoseconds. */
volatile long waitNanos;
Expand Down Expand Up @@ -109,17 +109,17 @@ boolean complete(Object result, Throwable failure, boolean checkArgs) {
circuitBreaker.recordSuccess();
}

// Determine the dynamic delay
long dynamicDelayNanos = -1;
// Determine the computed delay
long computedDelayNanos = -1;
DelayFunction<Object, Throwable> delayFunction = (DelayFunction<Object, Throwable>) retryPolicy.getDelayFn();
if (delayFunction != null && retryPolicy.canApplyDelayFn(result, failure)) {
Duration dynamicDelay = delayFunction.calculateDelay(result, failure, this);
if (dynamicDelay != null && dynamicDelay.toNanos() >= 0)
dynamicDelayNanos = dynamicDelay.toNanos();
Duration computedDelay = delayFunction.computeDelay(result, failure, this);
if (computedDelay != null && computedDelay.toNanos() >= 0)
computedDelayNanos = computedDelay.toNanos();
}

// Determine the non-dynamic delay
if (dynamicDelayNanos == -1) {
// Determine the non-computed delay
if (computedDelayNanos == -1) {
Duration delay = retryPolicy.getDelay();
Duration delayMin = retryPolicy.getDelayMin();
Duration delayMax = retryPolicy.getDelayMax();
Expand All @@ -134,7 +134,7 @@ else if (delayMin != null && delayMax != null)
delayNanos = (long) Math.min(delayNanos * retryPolicy.getDelayFactor(), retryPolicy.getMaxDelay().toNanos());
}

waitNanos = dynamicDelayNanos != -1 ? dynamicDelayNanos : delayNanos;
waitNanos = computedDelayNanos != -1 ? computedDelayNanos : delayNanos;

// Adjust the wait time for jitter
if (retryPolicy.getJitter() != null)
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/net/jodah/failsafe/RetryPolicy.java
Expand Up @@ -38,8 +38,7 @@
*/
public class RetryPolicy {
/**
* A functional interface for dynamically computing delays between retries in conjunction with
* {@link #withDelay(DelayFunction)}.
* A functional interface for computing delays between retries in conjunction with {@link #withDelay(DelayFunction)}.
*
* @param <R> result type
* @param <F> failure type
Expand Down Expand Up @@ -68,7 +67,7 @@ public interface DelayFunction<R, F extends Throwable> {
* @see #withDelayOn(DelayFunction, Class)
* @see #withDelayWhen(DelayFunction, Object)
*/
Duration calculateDelay(R result, F failure, ExecutionContext context);
Duration computeDelay(R result, F failure, ExecutionContext context);
}

static final RetryPolicy NEVER = new RetryPolicy().withMaxRetries(0);
Expand Down Expand Up @@ -527,7 +526,7 @@ public RetryPolicy withDelay(long delayMin, long delayMax, TimeUnit timeUnit) {
}

/**
* Sets the {@code delayFunction} that determines the next delay before retrying.
* Sets the {@code delayFunction} that computes the next delay before retrying.
*
* @param delayFunction the function to use to compute the delay before a next attempt
* @throws NullPointerException if {@code delayFunction} is null
Expand All @@ -540,7 +539,7 @@ public RetryPolicy withDelay(DelayFunction<?, ? extends Throwable> delayFunction
}

/**
* Sets the {@code delayFunction} that determines the next delay before retrying. Delays will only occur for failures
* Sets the {@code delayFunction} that computes the next delay before retrying. Delays will only occur for failures
* that are assignable from the {@code failure}.
*
* @param delayFunction the function to use to compute the delay before a next attempt
Expand All @@ -557,7 +556,7 @@ public <F extends Throwable> RetryPolicy withDelayOn(DelayFunction<Object, F> de
}

/**
* Sets the {@code delayFunction} that determines the next delay before retrying. Delays will only occur for results
* Sets the {@code delayFunction} that computes the next delay before retrying. Delays will only occur for results
* that equal the {@code result}.
*
* @param delayFunction the function to use to compute the delay before a next attempt
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/net/jodah/failsafe/ExecutionTest.java
Expand Up @@ -204,7 +204,7 @@ public void shouldAdjustWaitTimeForBackoff() {
assertEquals(exec.getWaitTime().toNanos(), 10);
}

public void shouldAdjustWaitTimeForDynamicDelay() {
public void shouldAdjustWaitTimeForComputedDelay() {
Execution exec = new Execution(
new RetryPolicy().withDelay((r, f, ctx) -> new Duration(ctx.getExecutions() * 2, TimeUnit.NANOSECONDS)));
assertEquals(exec.getWaitTime().toNanos(), 0);
Expand All @@ -218,7 +218,7 @@ public void shouldAdjustWaitTimeForDynamicDelay() {
assertEquals(exec.getWaitTime().toNanos(), 8);
}

public void shouldFallbackWaitTimeFromDynamicToFixedDelay() {
public void shouldFallbackWaitTimeFromComputedToFixedDelay() {
Execution exec = new Execution(new RetryPolicy().withDelay(5, TimeUnit.NANOSECONDS).withDelay((r, f,
ctx) -> new Duration(ctx.getExecutions() % 2 == 0 ? ctx.getExecutions() * 2 : -1, TimeUnit.NANOSECONDS)));
assertEquals(exec.getWaitTime().toNanos(), 0);
Expand All @@ -236,7 +236,7 @@ public void shouldFallbackWaitTimeFromDynamicToFixedDelay() {
assertEquals(exec.getWaitTime().toNanos(), 12);
}

public void shouldFallbackWaitTimeFromDynamicToBackoffDelay() {
public void shouldFallbackWaitTimeFromComputedToBackoffDelay() {
Execution exec = new Execution(new RetryPolicy().withBackoff(1, 10, TimeUnit.NANOSECONDS).withDelay((r, f,
ctx) -> new Duration(ctx.getExecutions() % 2 == 0 ? ctx.getExecutions() * 2 : -1, TimeUnit.NANOSECONDS)));
assertEquals(exec.getWaitTime().toNanos(), 0);
Expand Down
Expand Up @@ -28,7 +28,7 @@
import net.jodah.failsafe.util.Duration;

@Test
public class DynamicDelayTest {
public class ComputedDelayTest {
static class UncheckedExpectedException extends RuntimeException {
}

Expand Down

0 comments on commit ba34f8c

Please sign in to comment.