From 561acf6276c86f0d8d3b38a82048b855d00858a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A0=95=EC=9B=90=EC=8B=9D?= <30713548+WonSik36@users.noreply.github.com> Date: Wed, 19 Oct 2022 00:11:03 +0900 Subject: [PATCH] Disable loadbalancer clients retry when clients retry is disabled (fixed) (#773) --- ...RetryableFeignBlockingLoadBalancerClient.java | 7 +++++-- ...ableFeignBlockingLoadBalancerClientTests.java | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/loadbalancer/RetryableFeignBlockingLoadBalancerClient.java b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/loadbalancer/RetryableFeignBlockingLoadBalancerClient.java index c38a7532..2747b29a 100644 --- a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/loadbalancer/RetryableFeignBlockingLoadBalancerClient.java +++ b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/loadbalancer/RetryableFeignBlockingLoadBalancerClient.java @@ -65,6 +65,7 @@ import static org.springframework.cloud.openfeign.loadbalancer.LoadBalancerUtils * load-balanced with Spring Cloud LoadBalancer. * * @author Olga Maciaszek-Sharma + * @author Wonsik Cheung * @since 2.2.6 */ @SuppressWarnings({ "rawtypes", "unchecked" }) @@ -201,8 +202,10 @@ public class RetryableFeignBlockingLoadBalancerClient implements Client { retryTemplate.setListeners(retryListeners); } - retryTemplate.setRetryPolicy(retryPolicy == null ? new NeverRetryPolicy() - : new InterceptorRetryPolicy(toHttpRequest(request), retryPolicy, loadBalancerClient, serviceId)); + retryTemplate.setRetryPolicy( + !loadBalancerClientFactory.getProperties(serviceId).getRetry().isEnabled() || retryPolicy == null + ? new NeverRetryPolicy() : new InterceptorRetryPolicy(toHttpRequest(request), retryPolicy, + loadBalancerClient, serviceId)); return retryTemplate; } diff --git a/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/loadbalancer/RetryableFeignBlockingLoadBalancerClientTests.java b/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/loadbalancer/RetryableFeignBlockingLoadBalancerClientTests.java index f7bd3ab5..84310745 100644 --- a/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/loadbalancer/RetryableFeignBlockingLoadBalancerClientTests.java +++ b/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/loadbalancer/RetryableFeignBlockingLoadBalancerClientTests.java @@ -55,6 +55,7 @@ import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.argThat; import static org.mockito.ArgumentMatchers.eq; @@ -72,6 +73,7 @@ import static org.mockito.Mockito.when; * @see BlockingLoadBalancerClientTests * @author Olga Maciaszek-Sharma + * @author Wonsik Cheung */ @ExtendWith(MockitoExtension.class) class RetryableFeignBlockingLoadBalancerClientTests { @@ -160,6 +162,20 @@ class RetryableFeignBlockingLoadBalancerClientTests { verify(delegate, times(2)).execute(any(), any()); } + @Test + void shouldNotRetryOnDisabled() throws IOException { + properties.getRetry().setEnabled(false); + Request request = testRequest(); + when(delegate.execute(any(), any())).thenThrow(new IOException()); + when(retryFactory.createRetryPolicy(any(), eq(loadBalancerClient))) + .thenReturn(new BlockingLoadBalancedRetryPolicy(properties)); + + assertThatThrownBy(() -> feignBlockingLoadBalancerClient.execute(request, new Request.Options())) + .isInstanceOf(IOException.class); + + verify(delegate, times(1)).execute(any(), any()); + } + @Test void shouldExposeResponseBodyOnRetry() throws IOException { properties.getRetry().getRetryableStatusCodes().add(503);