Browse Source

Merge pull request #27071 from garyrussell

* pr/27071:
  Polish "Add maxAttempts to ExponentialBackOff"
  Add maxAttempts to ExponentialBackOff

Closes gh-27071
pull/31121/head
Stephane Nicoll 2 years ago
parent
commit
962f4d20b4
  1. 51
      spring-core/src/main/java/org/springframework/util/backoff/ExponentialBackOff.java
  2. 21
      spring-core/src/test/java/org/springframework/util/ExponentialBackOffTests.java

51
spring-core/src/main/java/org/springframework/util/backoff/ExponentialBackOff.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2020 the original author or authors. * Copyright 2002-2023 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -44,12 +44,14 @@ import org.springframework.util.Assert;
* 10 30000 * 10 30000
* </pre> * </pre>
* *
* <p>Note that the default max elapsed time is {@link Long#MAX_VALUE}. Use * <p>Note that the default max elapsed time and maximum number of attempts are both
* {@link #setMaxElapsedTime(long)} to limit the maximum length of time * {@link Long#MAX_VALUE}. Use {@link #setMaxElapsedTime(long)} to limit the maximum
* that an instance should accumulate before returning * length of time that an instance should accumulate before returning
* {@link BackOffExecution#STOP}. * {@link BackOffExecution#STOP}. Alternatively, use {@link #setMaxAttempts} to limit
* the number of attempts. The execution stops when any of those two limit is reached.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Gary Russell
* @since 4.1 * @since 4.1
*/ */
public class ExponentialBackOff implements BackOff { public class ExponentialBackOff implements BackOff {
@ -74,6 +76,10 @@ public class ExponentialBackOff implements BackOff {
*/ */
public static final long DEFAULT_MAX_ELAPSED_TIME = Long.MAX_VALUE; public static final long DEFAULT_MAX_ELAPSED_TIME = Long.MAX_VALUE;
/**
* The default maximum attempts.
*/
public static final int DEFAULT_MAX_ATTEMPTS = Integer.MAX_VALUE;
private long initialInterval = DEFAULT_INITIAL_INTERVAL; private long initialInterval = DEFAULT_INITIAL_INTERVAL;
@ -83,6 +89,8 @@ public class ExponentialBackOff implements BackOff {
private long maxElapsedTime = DEFAULT_MAX_ELAPSED_TIME; private long maxElapsedTime = DEFAULT_MAX_ELAPSED_TIME;
private int maxAttempts = DEFAULT_MAX_ATTEMPTS;
/** /**
* Create an instance with the default settings. * Create an instance with the default settings.
@ -90,6 +98,7 @@ public class ExponentialBackOff implements BackOff {
* @see #DEFAULT_MULTIPLIER * @see #DEFAULT_MULTIPLIER
* @see #DEFAULT_MAX_INTERVAL * @see #DEFAULT_MAX_INTERVAL
* @see #DEFAULT_MAX_ELAPSED_TIME * @see #DEFAULT_MAX_ELAPSED_TIME
* @see #DEFAULT_MAX_ATTEMPTS
*/ */
public ExponentialBackOff() { public ExponentialBackOff() {
} }
@ -152,6 +161,8 @@ public class ExponentialBackOff implements BackOff {
/** /**
* The maximum elapsed time in milliseconds after which a call to * The maximum elapsed time in milliseconds after which a call to
* {@link BackOffExecution#nextBackOff()} returns {@link BackOffExecution#STOP}. * {@link BackOffExecution#nextBackOff()} returns {@link BackOffExecution#STOP}.
* @param maxElapsedTime the maximum elapsed time
* @see #setMaxAttempts(int)
*/ */
public void setMaxElapsedTime(long maxElapsedTime) { public void setMaxElapsedTime(long maxElapsedTime) {
this.maxElapsedTime = maxElapsedTime; this.maxElapsedTime = maxElapsedTime;
@ -160,11 +171,35 @@ public class ExponentialBackOff implements BackOff {
/** /**
* Return the maximum elapsed time in milliseconds after which a call to * Return the maximum elapsed time in milliseconds after which a call to
* {@link BackOffExecution#nextBackOff()} returns {@link BackOffExecution#STOP}. * {@link BackOffExecution#nextBackOff()} returns {@link BackOffExecution#STOP}.
* @return the maximum elapsed time
* @see #getMaxAttempts()
*/ */
public long getMaxElapsedTime() { public long getMaxElapsedTime() {
return this.maxElapsedTime; return this.maxElapsedTime;
} }
/**
* The maximum number of attempts after which a call to
* {@link BackOffExecution#nextBackOff()} returns {@link BackOffExecution#STOP}.
* @param maxAttempts the maximum number of attempts.
* @since 6.1
* @see #setMaxElapsedTime(long)
*/
public void setMaxAttempts(int maxAttempts) {
this.maxAttempts = maxAttempts;
}
/**
* Return the maximum number of attempts after which a call to
* {@link BackOffExecution#nextBackOff()} returns {@link BackOffExecution#STOP}.
* @return the maximum number of attempts
* @since 6.1
* @see #getMaxElapsedTime()
*/
public int getMaxAttempts() {
return this.maxAttempts;
}
@Override @Override
public BackOffExecution start() { public BackOffExecution start() {
return new ExponentialBackOffExecution(); return new ExponentialBackOffExecution();
@ -182,14 +217,18 @@ public class ExponentialBackOff implements BackOff {
private long currentElapsedTime = 0; private long currentElapsedTime = 0;
private int attempts;
@Override @Override
public long nextBackOff() { public long nextBackOff() {
if (this.currentElapsedTime >= maxElapsedTime) { if (this.currentElapsedTime >= getMaxElapsedTime()
|| this.attempts >= getMaxAttempts()) {
return STOP; return STOP;
} }
long nextInterval = computeNextInterval(); long nextInterval = computeNextInterval();
this.currentElapsedTime += nextInterval; this.currentElapsedTime += nextInterval;
this.attempts++;
return nextInterval; return nextInterval;
} }

21
spring-core/src/test/java/org/springframework/util/ExponentialBackOffTests.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2023 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,6 +16,10 @@
package org.springframework.util; package org.springframework.util;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.util.backoff.BackOffExecution; import org.springframework.util.backoff.BackOffExecution;
@ -25,6 +29,8 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/** /**
* Tests for {@link ExponentialBackOff}.
*
* @author Stephane Nicoll * @author Stephane Nicoll
*/ */
class ExponentialBackOffTests { class ExponentialBackOffTests {
@ -131,4 +137,17 @@ class ExponentialBackOffTests {
assertThat(execution.toString()).isEqualTo("ExponentialBackOff{currentInterval=4000ms, multiplier=2.0}"); assertThat(execution.toString()).isEqualTo("ExponentialBackOff{currentInterval=4000ms, multiplier=2.0}");
} }
@Test
void maxAttempts() {
ExponentialBackOff backOff = new ExponentialBackOff();
backOff.setInitialInterval(1000L);
backOff.setMultiplier(2.0);
backOff.setMaxInterval(10000L);
backOff.setMaxAttempts(6);
List<Long> delays = new ArrayList<>();
BackOffExecution execution = backOff.start();
IntStream.range(0, 7).forEach(i -> delays.add(execution.nextBackOff()));
assertThat(delays).containsExactly(1000L, 2000L, 4000L, 8000L, 10000L, 10000L, BackOffExecution.STOP);
}
} }

Loading…
Cancel
Save