Polish "Add maxAttempts to ExponentialBackOff"
See gh-27071
This commit is contained in:
parent
f6a09f3fad
commit
609580bfb9
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2021 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,11 +44,11 @@ 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}. Or use {@link #setMaxAttempts} to limit
|
* {@link BackOffExecution#STOP}. Alternatively, use {@link #setMaxAttempts} to limit
|
||||||
* the number of attempts.
|
* the number of attempts. The execution stops when any of those two limit is reached.
|
||||||
*
|
*
|
||||||
* @author Stephane Nicoll
|
* @author Stephane Nicoll
|
||||||
* @author Gary Russell
|
* @author Gary Russell
|
||||||
|
@ -161,7 +161,7 @@ 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 maxElapsedTime.
|
* @param maxElapsedTime the maximum elapsed time
|
||||||
* @see #setMaxAttempts(int)
|
* @see #setMaxAttempts(int)
|
||||||
*/
|
*/
|
||||||
public void setMaxElapsedTime(long maxElapsedTime) {
|
public void setMaxElapsedTime(long maxElapsedTime) {
|
||||||
|
@ -171,7 +171,7 @@ 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 maxElapsedTime.
|
* @return the maximum elapsed time
|
||||||
* @see #getMaxAttempts()
|
* @see #getMaxAttempts()
|
||||||
*/
|
*/
|
||||||
public long getMaxElapsedTime() {
|
public long getMaxElapsedTime() {
|
||||||
|
@ -181,8 +181,8 @@ public class ExponentialBackOff implements BackOff {
|
||||||
/**
|
/**
|
||||||
* The maximum number of attempts after which a call to
|
* The maximum number of attempts after which a call to
|
||||||
* {@link BackOffExecution#nextBackOff()} returns {@link BackOffExecution#STOP}.
|
* {@link BackOffExecution#nextBackOff()} returns {@link BackOffExecution#STOP}.
|
||||||
* @param maxAttempts the maxAttempts.
|
* @param maxAttempts the maximum number of attempts.
|
||||||
* @since 5.3.8
|
* @since 6.1
|
||||||
* @see #setMaxElapsedTime(long)
|
* @see #setMaxElapsedTime(long)
|
||||||
*/
|
*/
|
||||||
public void setMaxAttempts(int maxAttempts) {
|
public void setMaxAttempts(int maxAttempts) {
|
||||||
|
@ -192,8 +192,8 @@ public class ExponentialBackOff implements BackOff {
|
||||||
/**
|
/**
|
||||||
* Return the maximum number of attempts after which a call to
|
* Return the maximum number of attempts after which a call to
|
||||||
* {@link BackOffExecution#nextBackOff()} returns {@link BackOffExecution#STOP}.
|
* {@link BackOffExecution#nextBackOff()} returns {@link BackOffExecution#STOP}.
|
||||||
* @return the maxAttempts.
|
* @return the maximum number of attempts
|
||||||
* @since 5.3.8
|
* @since 6.1
|
||||||
* @see #getMaxElapsedTime()
|
* @see #getMaxElapsedTime()
|
||||||
*/
|
*/
|
||||||
public int getMaxAttempts() {
|
public int getMaxAttempts() {
|
||||||
|
@ -221,7 +221,8 @@ public class ExponentialBackOff implements BackOff {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long nextBackOff() {
|
public long nextBackOff() {
|
||||||
if (this.currentElapsedTime >= maxElapsedTime || this.attempts >= maxAttempts) {
|
if (this.currentElapsedTime >= getMaxElapsedTime()
|
||||||
|
|| this.attempts >= getMaxAttempts()) {
|
||||||
return STOP;
|
return STOP;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2021 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.
|
||||||
|
@ -29,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 {
|
||||||
|
@ -137,15 +139,15 @@ class ExponentialBackOffTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void maxAttempts() {
|
void maxAttempts() {
|
||||||
ExponentialBackOff bo = new ExponentialBackOff();
|
ExponentialBackOff backOff = new ExponentialBackOff();
|
||||||
bo.setInitialInterval(1_000L);
|
backOff.setInitialInterval(1000L);
|
||||||
bo.setMultiplier(2.0);
|
backOff.setMultiplier(2.0);
|
||||||
bo.setMaxInterval(10_000L);
|
backOff.setMaxInterval(10000L);
|
||||||
bo.setMaxAttempts(6);
|
backOff.setMaxAttempts(6);
|
||||||
List<Long> delays = new ArrayList<>();
|
List<Long> delays = new ArrayList<>();
|
||||||
BackOffExecution boEx = bo.start();
|
BackOffExecution execution = backOff.start();
|
||||||
IntStream.range(0, 7).forEach(i -> delays.add(boEx.nextBackOff()));
|
IntStream.range(0, 7).forEach(i -> delays.add(execution.nextBackOff()));
|
||||||
assertThat(delays).containsExactly(1_000L, 2_000L, 4_000L, 8_000L, 10_000L, 10_000L, -1L);
|
assertThat(delays).containsExactly(1000L, 2000L, 4000L, 8000L, 10000L, 10000L, BackOffExecution.STOP);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue