Skip to content

Commit 471c5c5

Browse files
committed
Merge remote-tracking branch 'origin/3.1.x'
2 parents 874ff0d + 23ca17d commit 471c5c5

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/loadbalancer/RetryableFeignBlockingLoadBalancerClient.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2022 the original author or authors.
2+
* Copyright 2013-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -68,6 +68,7 @@
6868
* @author Olga Maciaszek-Sharma
6969
* @author changjin wei(魏昌进)
7070
* @author Wonsik Cheung
71+
* @author Andriy Pikozh
7172
* @since 2.2.6
7273
*/
7374
@SuppressWarnings({ "rawtypes", "unchecked" })
@@ -145,8 +146,8 @@ public Response execute(Request request, Request.Options options) throws IOExcep
145146
// On retries the policy will choose the server and set it in the context
146147
// and extract the server and update the request being made
147148
if (context instanceof LoadBalancedRetryContext lbContext) {
148-
ServiceInstance serviceInstance = lbContext.getServiceInstance();
149-
if (serviceInstance == null) {
149+
retrievedServiceInstance = lbContext.getServiceInstance();
150+
if (retrievedServiceInstance == null) {
150151
if (LOG.isDebugEnabled()) {
151152
LOG.debug("Service instance retrieved from LoadBalancedRetryContext: was null. "
152153
+ "Reattempting service instance selection");

spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/loadbalancer/RetryableFeignBlockingLoadBalancerClientTests.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2022 the original author or authors.
2+
* Copyright 2013-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -75,6 +75,7 @@
7575
* @author Olga Maciaszek-Sharma
7676
* @author changjin wei(魏昌进)
7777
* @author Wonsik Cheung
78+
* @author Andriy Pikozh
7879
* @see <a href=
7980
* "https://github.com/spring-cloud/spring-cloud-commons/blob/main/spring-cloud-loadbalancer/src/test/java/org/springframework/cloud/loadbalancer/blocking/client/BlockingLoadBalancerClientTests.java">BlockingLoadBalancerClientTests</a>
8081
*/
@@ -164,10 +165,51 @@ void shouldRetryOnRepeatableStatusCode() throws IOException {
164165

165166
feignBlockingLoadBalancerClient.execute(request, new Request.Options());
166167

168+
verify(loadBalancerClient, times(2)).choose(eq("test"), any());
167169
verify(loadBalancerClient, times(2)).reconstructURI(serviceInstance, URI.create("http://test/path"));
168170
verify(delegate, times(2)).execute(any(), any());
169171
}
170172

173+
@Test
174+
void shouldReuseServerInstanceOnSameInstanceRetry() throws IOException {
175+
properties.getRetry().setMaxRetriesOnSameServiceInstance(1);
176+
properties.getRetry().setMaxRetriesOnNextServiceInstance(0);
177+
properties.getRetry().getRetryableStatusCodes().add(503);
178+
Request request = testRequest();
179+
Response response = testResponse(503);
180+
when(delegate.execute(any(), any())).thenReturn(response);
181+
when(retryFactory.createRetryPolicy(any(), eq(loadBalancerClient)))
182+
.thenReturn(new BlockingLoadBalancedRetryPolicy(properties));
183+
when(loadBalancerClient.reconstructURI(serviceInstance, URI.create("http://test/path")))
184+
.thenReturn(URI.create("http://testhost:80/path"));
185+
186+
feignBlockingLoadBalancerClient.execute(request, new Request.Options());
187+
188+
verify(loadBalancerClient, times(1)).choose(eq("test"), any());
189+
verify(loadBalancerClient, times(2)).reconstructURI(serviceInstance, URI.create("http://test/path"));
190+
verify(delegate, times(2)).execute(any(), any());
191+
}
192+
193+
@Test
194+
void shouldReuseServerInstanceOnSameInstanceRetryWithBothSameAndNextRetries() throws IOException {
195+
properties.getRetry().setMaxRetriesOnSameServiceInstance(1);
196+
properties.getRetry().setMaxRetriesOnNextServiceInstance(1);
197+
properties.getRetry().getRetryableStatusCodes().add(503);
198+
Request request = testRequest();
199+
Response response = testResponse(503);
200+
when(delegate.execute(any(), any())).thenReturn(response);
201+
when(retryFactory.createRetryPolicy(any(), eq(loadBalancerClient)))
202+
.thenReturn(new BlockingLoadBalancedRetryPolicy(properties));
203+
when(loadBalancerClient.reconstructURI(serviceInstance, URI.create("http://test/path")))
204+
.thenReturn(URI.create("http://testhost:80/path"));
205+
206+
feignBlockingLoadBalancerClient.execute(request, new Request.Options());
207+
208+
verify(loadBalancerClient, times(2)).choose(eq("test"), any());
209+
verify(loadBalancerClient, times(4)).reconstructURI(serviceInstance, URI.create("http://test/path"));
210+
verify(delegate, times(4)).execute(any(), any());
211+
}
212+
171213
@Test
172214
void shouldNotRetryOnDisabled() throws IOException {
173215
properties.getRetry().setEnabled(false);

0 commit comments

Comments
 (0)