Add support for random exponential backoff policy. Fixes #2351 (#2353)

Co-authored-by: Ryan Baxter <524254+ryanjbaxter@users.noreply.github.com>
This commit is contained in:
Ryan Baxter
2023-11-21 15:23:27 -05:00
committed by GitHub
parent 613be43082
commit 0b92d82a2b
4 changed files with 19 additions and 3 deletions

View File

@@ -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.

View File

@@ -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;
}
@@ -82,4 +87,12 @@ public class RetryProperties {
this.maxAttempts = maxAttempts;
}
public boolean isUseRandomPolicy() {
return this.useRandomPolicy;
}
public void setUseRandomPolicy(boolean useRandomPolicy) {
this.useRandomPolicy = useRandomPolicy;
}
}

View File

@@ -39,9 +39,9 @@ public final class 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);

View File

@@ -138,6 +138,7 @@ public class ConfigServerConfigDataLocationResolverTests {
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
@@ -158,6 +159,7 @@ public class ConfigServerConfigDataLocationResolverTests {
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