Skip to content

Commit

Permalink
Add support for random exponential backoff policy. Fixes #2351 (#2353)
Browse files Browse the repository at this point in the history
Co-authored-by: Ryan Baxter <[email protected]>
  • Loading branch information
Ryan Baxter and ryanjbaxter authored Nov 21, 2023
1 parent 613be43 commit 0b92d82
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/src/main/asciidoc/spring-cloud-config.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1906,6 +1906,7 @@ First, you need to set `spring.cloud.config.fail-fast=true`.
Then you need to add `spring-retry` and `spring-boot-starter-aop` to your classpath.
The default behavior is to retry six times with an initial backoff interval of 1000ms and an exponential multiplier of 1.1 for subsequent backoffs.
You can configure these properties (and others) by setting the `spring.cloud.config.retry.*` configuration properties.
To use a random exponential backoff policy set `spring.cloud.config.retry.useRandomPolicy` to `true`.

TIP: To take full control of the retry behavior and are using legacy bootstrap, add a `@Bean` of type `RetryOperationsInterceptor` with an ID of `configServerRetryInterceptor`.
Spring Retry has a `RetryInterceptorBuilder` that supports creating one.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public class RetryProperties {
*/
int maxAttempts = 6;

/**
* Use a random exponential backoff policy.
*/
boolean useRandomPolicy = false;

public long getInitialInterval() {
return this.initialInterval;
}
Expand Down Expand Up @@ -82,4 +87,12 @@ public void setMaxAttempts(int maxAttempts) {
this.maxAttempts = maxAttempts;
}

public boolean isUseRandomPolicy() {
return this.useRandomPolicy;
}

public void setUseRandomPolicy(boolean useRandomPolicy) {
this.useRandomPolicy = useRandomPolicy;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ private RetryTemplateFactory() {
}

public static RetryTemplate create(RetryProperties properties, Log log) {
RetryTemplate retryTemplate = RetryTemplate.builder().maxAttempts(properties.getMaxAttempts())
.exponentialBackoff(properties.getInitialInterval(), properties.getMultiplier(),
properties.getMaxInterval())
RetryTemplate retryTemplate = RetryTemplate
.builder().maxAttempts(properties.getMaxAttempts()).exponentialBackoff(properties.getInitialInterval(),
properties.getMultiplier(), properties.getMaxInterval(), properties.isUseRandomPolicy())
.build();
try {
field.set(retryTemplate, log);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ void retryPropertiesShouldBeDefaultByDefault() {
assertThat(resource.getRetryProperties().getMaxInterval()).isEqualTo(defaultRetry.getMaxInterval());
assertThat(resource.getRetryProperties().getInitialInterval()).isEqualTo(defaultRetry.getInitialInterval());
assertThat(resource.getRetryProperties().getMultiplier()).isEqualTo(defaultRetry.getMultiplier());
assertThat(resource.getRetryProperties().isUseRandomPolicy()).isEqualTo(defaultRetry.isUseRandomPolicy());
}

@Test
Expand All @@ -158,6 +159,7 @@ void uriWithParamsParsesProperties() {
assertThat(resource.getRetryProperties().getMaxInterval()).isEqualTo(1500);
assertThat(resource.getRetryProperties().getInitialInterval()).isEqualTo(1100);
assertThat(resource.getRetryProperties().getMultiplier()).isEqualTo(1.2);
assertThat(resource.getRetryProperties().isUseRandomPolicy()).isEqualTo(false);
}

@Test
Expand Down

0 comments on commit 0b92d82

Please sign in to comment.