Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduced RetryPolicyBuilder.withBackoffResetAfter #342

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions core/src/main/java/dev/failsafe/RetryPolicyBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,16 @@ public RetryPolicyBuilder<R> withBackoff(Duration delay, Duration maxDelay, doub
return this;
}

/**
* If a backoff delay factor is given, the delay will be reset to the initial delay when a task attempt has been
* running without error for the given duration. This can be used for long-running tasks that can continue their work
* after a period with network issues for example.
*/
public RetryPolicyBuilder<R> withBackoffResetAfter(Duration backoffReset) {
config.backoffReset = backoffReset;
return this;
}

/**
* Sets the {@code delay} to occur between retries. Replaces any previously configured {@link #withBackoff(Duration,
* Duration) backoff} or {@link #withDelay(Duration, Duration) random} delays.
Expand Down
11 changes: 11 additions & 0 deletions core/src/main/java/dev/failsafe/RetryPolicyConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class RetryPolicyConfig<R> extends DelayablePolicyConfig<R> {
Duration delayMin;
Duration delayMax;
double delayFactor;
Duration backoffReset;
Duration maxDelay;
Duration jitter;
double jitterFactor;
Expand All @@ -63,6 +64,7 @@ public class RetryPolicyConfig<R> extends DelayablePolicyConfig<R> {
delayMin = config.delayMin;
delayMax = config.delayMax;
delayFactor = config.delayFactor;
backoffReset = config.backoffReset;
maxDelay = config.maxDelay;
jitter = config.jitter;
jitterFactor = config.jitterFactor;
Expand Down Expand Up @@ -146,6 +148,15 @@ public double getDelayFactor() {
return delayFactor;
}

/**
* Returns the duration after which the backoff delay will be reset
*
* @see RetryPolicyBuilder#withBackoffResetAfter(Duration)
*/
public Duration getBackoffReset() {
return backoffReset;
}

/**
* Returns the jitter, else {@code null} if none has been configured.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,9 @@ else if (delayMin != null && delayMax != null)
}

private long adjustForBackoff(ExecutionContext<R> context, long delayNanos) {
if (config.getBackoffReset() != null && context.getElapsedAttemptTime().compareTo(config.getBackoffReset()) >= 0) {
return config.getDelay().toNanos();
}
if (context.getAttemptCount() != 1 && config.getMaxDelay() != null)
delayNanos = (long) Math.min(delayNanos * config.getDelayFactor(), config.getMaxDelay().toNanos());
return delayNanos;
Expand Down