Skip to content

Commit 61201db

Browse files
committed
Improve error message for preemptive timeout in RetryTemplate
The error message in such cases now indicates that the retry process is being aborted preemptively due to pending sleep time. For example: Retry policy for operation 'myMethod' would exceed timeout (5 ms) due to pending sleep time (10 ms); preemptively aborting execution See gh-35963
1 parent 2643c62 commit 61201db

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

spring-core/src/main/java/org/springframework/core/retry/RetryTemplate.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,13 @@ private void checkIfTimeoutExceeded(long timeout, long startTime, long sleepTime
213213
// would be if we were to sleep for sleepTime milliseconds.
214214
long elapsedTime = System.currentTimeMillis() + sleepTime - startTime;
215215
if (elapsedTime >= timeout) {
216-
RetryException retryException = new RetryException(
216+
String message = (sleepTime > 0 ? """
217+
Retry policy for operation '%s' would exceed timeout (%d ms) due \
218+
to pending sleep time (%d ms); preemptively aborting execution"""
219+
.formatted(retryable.getName(), timeout, sleepTime) :
217220
"Retry policy for operation '%s' exceeded timeout (%d ms); aborting execution"
218-
.formatted(retryable.getName(), timeout), exceptions.removeLast());
221+
.formatted(retryable.getName(), timeout));
222+
RetryException retryException = new RetryException(message, exceptions.removeLast());
219223
exceptions.forEach(retryException::addSuppressed);
220224
this.retryListener.onRetryPolicyTimeout(this.retryPolicy, retryable, retryException);
221225
throw retryException;

spring-core/src/test/java/org/springframework/core/retry/RetryTemplateTests.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,10 @@ void retryWithTimeoutExceededAfterFirstDelayButBeforeFirstRetry() throws Excepti
502502
assertThat(invocationCount).hasValue(0);
503503
assertThatExceptionOfType(RetryException.class)
504504
.isThrownBy(() -> retryTemplate.execute(retryable))
505-
.withMessageMatching("Retry policy for operation '.+?' exceeded timeout \\(5 ms\\); aborting execution")
505+
.withMessageMatching("""
506+
Retry policy for operation '.+?' would exceed timeout \\(5 ms\\) \
507+
due to pending sleep time \\(10 ms\\); preemptively aborting execution\
508+
""")
506509
.withCause(new CustomException("Boom 1"))
507510
.satisfies(throwable -> inOrder.verify(retryListener).onRetryPolicyTimeout(
508511
eq(retryPolicy), eq(retryable), eq(throwable)));

0 commit comments

Comments
 (0)