Add maxAttempts to ExponentialBackOff
If you wish to stop after a certain number of attempts with an `ExponentialBackOff` you have to calculate the `maxElapsedTime` corresponding to the number of attempts. Add a new property to make it more convenient to stop after a certain number of attempts. See gh-27071
This commit is contained in:
committed by
Stephane Nicoll
parent
48878619d2
commit
f6a09f3fad
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2021 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,6 +16,10 @@
|
||||
|
||||
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.springframework.util.backoff.BackOffExecution;
|
||||
@@ -131,4 +135,17 @@ class ExponentialBackOffTests {
|
||||
assertThat(execution.toString()).isEqualTo("ExponentialBackOff{currentInterval=4000ms, multiplier=2.0}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void maxAttempts() {
|
||||
ExponentialBackOff bo = new ExponentialBackOff();
|
||||
bo.setInitialInterval(1_000L);
|
||||
bo.setMultiplier(2.0);
|
||||
bo.setMaxInterval(10_000L);
|
||||
bo.setMaxAttempts(6);
|
||||
List<Long> delays = new ArrayList<>();
|
||||
BackOffExecution boEx = bo.start();
|
||||
IntStream.range(0, 7).forEach(i -> delays.add(boEx.nextBackOff()));
|
||||
assertThat(delays).containsExactly(1_000L, 2_000L, 4_000L, 8_000L, 10_000L, 10_000L, -1L);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user