Skip to content

Commit 635dad7

Browse files
committed
#4567: Enhances retry configuration for network errors
Extends the retry configuration to include `ResourceAccessException` and, if available, `WebClientRequestException` to handle transient network errors during AI service calls more effectively. This improves the resilience of the application when interacting with potentially unstable AI services. Signed-off-by: Seunggyu Lee <[email protected]>
1 parent 7d0aff0 commit 635dad7

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

auto-configurations/common/spring-ai-autoconfigure-retry/src/main/java/org/springframework/ai/retry/autoconfigure/SpringAiRetryAutoConfiguration.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import org.slf4j.Logger;
2323
import org.slf4j.LoggerFactory;
24-
2524
import org.springframework.ai.retry.NonTransientAiException;
2625
import org.springframework.ai.retry.RetryUtils;
2726
import org.springframework.ai.retry.TransientAiException;
@@ -36,8 +35,10 @@
3635
import org.springframework.retry.RetryContext;
3736
import org.springframework.retry.RetryListener;
3837
import org.springframework.retry.support.RetryTemplate;
38+
import org.springframework.retry.support.RetryTemplateBuilder;
3939
import org.springframework.util.CollectionUtils;
4040
import org.springframework.util.StreamUtils;
41+
import org.springframework.web.client.ResourceAccessException;
4142
import org.springframework.web.client.ResponseErrorHandler;
4243

4344
/**
@@ -47,6 +48,7 @@
4748
*
4849
* @author Christian Tzolov
4950
* @author SriVarshan P
51+
* @author Seunggyu Lee
5052
*/
5153
@AutoConfiguration
5254
@ConditionalOnClass(RetryUtils.class)
@@ -58,9 +60,10 @@ public class SpringAiRetryAutoConfiguration {
5860
@Bean
5961
@ConditionalOnMissingBean
6062
public RetryTemplate retryTemplate(SpringAiRetryProperties properties) {
61-
return RetryTemplate.builder()
63+
RetryTemplateBuilder builder = RetryTemplate.builder()
6264
.maxAttempts(properties.getMaxAttempts())
6365
.retryOn(TransientAiException.class)
66+
.retryOn(ResourceAccessException.class)
6467
.exponentialBackoff(properties.getBackoff().getInitialInterval(), properties.getBackoff().getMultiplier(),
6568
properties.getBackoff().getMaxInterval())
6669
.withListener(new RetryListener() {
@@ -71,8 +74,21 @@ public <T, E extends Throwable> void onError(RetryContext context, RetryCallback
7174
logger.warn("Retry error. Retry count: {}, Exception: {}", context.getRetryCount(),
7275
throwable.getMessage(), throwable);
7376
}
74-
})
75-
.build();
77+
});
78+
79+
// Optionally add WebFlux pre-response network errors if present without hard dependency
80+
try {
81+
Class<?> webClientRequestEx = Class
82+
.forName("org.springframework.web.reactive.function.client.WebClientRequestException");
83+
@SuppressWarnings("unchecked")
84+
Class<? extends Throwable> exClass = (Class<? extends Throwable>) webClientRequestEx;
85+
builder.retryOn(exClass);
86+
}
87+
catch (ClassNotFoundException ignore) {
88+
// WebFlux not on classpath; skip
89+
}
90+
91+
return builder.build();
7692
}
7793

7894
@Bean

0 commit comments

Comments
 (0)