generated from hmcts/spring-boot-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LAU-1090 replace custom retry with native openfeign retry (#408)
* LAU-1090 replace custom retry with native openfeign retry
- Loading branch information
1 parent
a917f15
commit a584c3c
Showing
13 changed files
with
130 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 0 additions & 53 deletions
53
src/main/java/uk/gov/hmcts/reform/idam/service/aop/MethodRetryAspect.java
This file was deleted.
Oops, something went wrong.
13 changes: 0 additions & 13 deletions
13
src/main/java/uk/gov/hmcts/reform/idam/service/aop/Retry.java
This file was deleted.
Oops, something went wrong.
28 changes: 28 additions & 0 deletions
28
src/main/java/uk/gov/hmcts/reform/idam/service/remote/CustomFeignErrorDecoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package uk.gov.hmcts.reform.idam.service.remote; | ||
|
||
import feign.FeignException; | ||
import feign.Response; | ||
import feign.RetryableException; | ||
import feign.codec.ErrorDecoder; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
public class CustomFeignErrorDecoder implements ErrorDecoder { | ||
|
||
@Override | ||
public Exception decode(String methodKey, Response response) { | ||
int status = response.status(); | ||
FeignException exception = FeignException.errorStatus(methodKey, response); | ||
log.info("Feign response status: {}, message - {}", status, exception.getMessage()); | ||
if (status >= 400) { | ||
return new RetryableException( | ||
response.status(), | ||
exception.getMessage(), | ||
response.request().httpMethod(), | ||
(Long) null, // unix timestamp, if given retries after that point in time | ||
response.request() | ||
); | ||
} | ||
return exception; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/uk/gov/hmcts/reform/idam/service/remote/RetryableFeignConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package uk.gov.hmcts.reform.idam.service.remote; | ||
|
||
import feign.Retryer; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import static java.util.concurrent.TimeUnit.SECONDS; | ||
|
||
@Configuration | ||
public class RetryableFeignConfig { | ||
|
||
@Value("${service.feign-retry-min-wait:60}") | ||
private int periodToWait; | ||
|
||
@Bean | ||
public Retryer retryer() { | ||
long period = SECONDS.toMillis(periodToWait); | ||
long maxPeriod = SECONDS.toMillis(300); | ||
// default backoff calculation formula: | ||
// Math.min(period * Math.pow(1.5, attempt - 1), maxPeriod) | ||
return new Retryer.Default(period, maxPeriod, 3); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 0 additions & 93 deletions
93
src/test/java/uk/gov/hmcts/reform/idam/service/aop/MethodRetryAspectTest.java
This file was deleted.
Oops, something went wrong.
53 changes: 53 additions & 0 deletions
53
src/test/java/uk/gov/hmcts/reform/idam/service/remote/CustomFeignErrorDecoderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package uk.gov.hmcts.reform.idam.service.remote; | ||
|
||
import feign.Request; | ||
import feign.RequestTemplate; | ||
import feign.Response; | ||
import feign.RetryableException; | ||
import feign.codec.ErrorDecoder; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
|
||
import java.util.Map; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class CustomFeignErrorDecoderTest { | ||
|
||
private final ErrorDecoder errorDecoder = new CustomFeignErrorDecoder(); | ||
|
||
@ParameterizedTest | ||
@CsvSource({ | ||
"400, Bad Request", | ||
"401, Unauthorized", | ||
"403, Forbidden", | ||
"500, Internal Server Error", | ||
"502, Bad Gateway", | ||
"503, Service Unavailable", | ||
"504, Gateway Timeout" | ||
}) | ||
void testDecodeErrorReturnsRetryable(int status, String reason) { | ||
|
||
Response response = Response.builder() | ||
.status(status) | ||
.reason(reason) | ||
.request(Request.create(Request.HttpMethod.GET, "/", Map.of(), null, new RequestTemplate())) | ||
.build(); | ||
Exception exc = errorDecoder.decode("methodKey", response); | ||
assertThat(exc).isInstanceOf(RetryableException.class); | ||
} | ||
|
||
@Test | ||
void testDecodeNonRetryableException() { | ||
Response response = Response.builder() | ||
.status(302) | ||
.reason("Found") | ||
.request(Request.create(Request.HttpMethod.GET, "/", Map.of(), null, new RequestTemplate())) | ||
.build(); | ||
Exception exc = errorDecoder.decode("methodKey", response); | ||
assertThat(exc) | ||
.isInstanceOf(feign.FeignException.class) | ||
.isNotInstanceOf(RetryableException.class); | ||
} | ||
} |