-
Notifications
You must be signed in to change notification settings - Fork 11
feat: add linear retry strategy #468
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,9 @@ public static class Presets { | |
| JitterStrategy.FULL // jitter | ||
| ); | ||
|
|
||
| /** Linear retry strategy: 6 total attempts (1 initial + 5 retries) with 1s, 2s, 3s, 4s, and 5s delays. */ | ||
| public static final RetryStrategy LINEAR = linearBackoff(6, Duration.ofSeconds(1), Duration.ofSeconds(1)); | ||
|
|
||
| /** No retry strategy - fails immediately on first error. Use this for operations that should not be retried. */ | ||
| public static final RetryStrategy NO_RETRY = (error, attempt) -> RetryDecision.fail(); | ||
| } | ||
|
|
@@ -79,6 +82,33 @@ public static RetryStrategy exponentialBackoff( | |
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Creates a linear backoff retry strategy. | ||
| * | ||
| * <p>The delay calculation follows the formula: delay = initialDelay + increment × (attempt-1) | ||
| * | ||
| * @param maxAttempts Maximum number of attempts (including initial attempt) | ||
| * @param initialDelay Initial delay before first retry | ||
| * @param increment Amount to add to the delay after each retry attempt | ||
| * @return RetryStrategy implementing linear backoff | ||
| */ | ||
| public static RetryStrategy linearBackoff(int maxAttempts, Duration initialDelay, Duration increment) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be more consistent with
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want a jitter for this strategy?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nevermind, they're aligned now #652
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, basically the preset |
||
| if (maxAttempts <= 0) { | ||
| throw new IllegalArgumentException("maxAttempts must be positive"); | ||
| } | ||
| ParameterValidator.validateDuration(initialDelay, "initialDelay"); | ||
| ParameterValidator.validateDuration(increment, "increment"); | ||
|
|
||
| return (error, attempt) -> { | ||
| if (attempt >= maxAttempts) { | ||
| return RetryDecision.fail(); | ||
| } | ||
|
|
||
| var delay = initialDelay.plus(increment.multipliedBy(attempt - 1)); | ||
| return RetryDecision.retry(delay); | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Creates a simple retry strategy that retries a fixed number of times with a fixed delay. | ||
| * | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should match the format (with comments ) of the Default above.