This repository was archived by the owner on Jan 11, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
[REFACTOR] Feign 재시도 및 예외 추가 설정 #35
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
15 changes: 15 additions & 0 deletions
15
src/main/java/com/example/spot/common/config/FeignConfig.java
This file contains hidden or 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 |
|---|---|---|
| @@ -1,9 +1,24 @@ | ||
| package com.example.spot.common.config; | ||
|
|
||
| import com.example.spot.common.infrastructure.feign.retry.SelectiveRetryErrorDecoder; | ||
| import feign.Retryer; | ||
| import feign.codec.ErrorDecoder; | ||
| import org.springframework.cloud.openfeign.EnableFeignClients; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| @EnableFeignClients(basePackages = "com.example.spot") | ||
| @Configuration | ||
| public class FeignConfig { | ||
|
|
||
| @Bean | ||
| public ErrorDecoder errorDecoder() { | ||
| return new SelectiveRetryErrorDecoder(); | ||
| } | ||
|
|
||
| @Bean | ||
| public Retryer retryer() { | ||
| // 초기대기 200ms, 최대대기 800ms, 최대 3번 재시도 | ||
| return new Retryer.Default(200, 800, 3); | ||
| } | ||
| } | ||
14 changes: 0 additions & 14 deletions
14
src/main/java/com/example/spot/common/config/FeignRetryConfig.java
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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
66 changes: 66 additions & 0 deletions
66
...n/java/com/example/spot/common/infrastructure/feign/retry/SelectiveRetryErrorDecoder.java
This file contains hidden or 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,66 @@ | ||
| package com.example.spot.common.infrastructure.feign.retry; | ||
|
|
||
| import feign.Response; | ||
| import feign.RetryableException; | ||
| import feign.codec.ErrorDecoder; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import java.util.Map; | ||
|
|
||
| public class SelectiveRetryErrorDecoder implements ErrorDecoder { | ||
|
|
||
| public static final String GET = "GET"; | ||
| public static final String HEAD = "HEAD"; | ||
| public static final String OPTIONS = "OPTIONS"; | ||
| public static final String RETRYABLE_STATUS = "retryable status "; | ||
| public static final String RETRY_AFTER = "Retry-After"; | ||
| public static final long THRESHOLD = 1000L; | ||
|
|
||
| private final ErrorDecoder defaultDecoder = new Default(); | ||
|
|
||
| @Override | ||
| public Exception decode(String methodKey, Response response) { | ||
| int status = response.status(); | ||
|
|
||
| // 재시도 대상 상태코드 | ||
| boolean retryableStatus = status == 429 || status == 408 || (status >= 500 && status <= 599); | ||
|
|
||
| // 멱등 메서드만 재시도 (GET/HEAD/OPTIONS) | ||
| String method = response.request().httpMethod().name(); | ||
| boolean idempotent = method.equals(GET) || method.equals(HEAD) || method.equals(OPTIONS); | ||
| if (!(retryableStatus && idempotent)) { | ||
| return defaultDecoder.decode(methodKey, response); | ||
| } | ||
|
|
||
| Long retryAfter = extractRetryAfter(response.headers()); | ||
| return new RetryableException(status, RETRYABLE_STATUS + status, response.request().httpMethod(), | ||
| retryAfter, response.request(), readBody(response), response.headers()); | ||
| } | ||
|
|
||
| private Long extractRetryAfter(Map<String, Collection<String>> headers) { | ||
| Collection<String> value = headers.getOrDefault(RETRY_AFTER, Collections.emptyList()); | ||
| if (value.isEmpty()) { | ||
| return null; | ||
| } | ||
| String v = value.iterator().next().trim(); | ||
| try { | ||
| long seconds = Long.parseLong(v); | ||
| return System.currentTimeMillis() + (seconds * THRESHOLD); | ||
| } catch (NumberFormatException ignore) { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| private byte[] readBody(Response response) { | ||
| if (response.body() == null) { | ||
| return null; | ||
| } | ||
| try (InputStream in = response.body().asInputStream()) { | ||
| return in.readAllBytes(); | ||
| } catch (IOException e) { | ||
| return null; | ||
| } | ||
| } | ||
| } |
71 changes: 71 additions & 0 deletions
71
src/test/java/com/example/spot/common/feign/SelectiveRetryErrorDecoderTest.java
This file contains hidden or 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,71 @@ | ||
| package com.example.spot.common.feign; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import com.example.spot.common.infrastructure.feign.retry.SelectiveRetryErrorDecoder; | ||
| import feign.Request; | ||
| import feign.Response; | ||
| import feign.RetryableException; | ||
| import feign.codec.ErrorDecoder; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Collection; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class SelectiveRetryErrorDecoderTest { | ||
|
|
||
| private final ErrorDecoder decoder = new SelectiveRetryErrorDecoder(); | ||
|
|
||
| private Response newResponse(int status, String method, Map<String, Collection<String>> headers, String body) { | ||
| Request req = Request.create( | ||
| Request.HttpMethod.valueOf(method), | ||
| "https://api.test/resource", | ||
| Map.of(), // request headers | ||
| null, // body | ||
| StandardCharsets.UTF_8, | ||
| null | ||
| ); | ||
|
|
||
| Response.Builder builder = Response.builder() | ||
| .status(status) | ||
| .reason("test") | ||
| .headers(headers == null ? Map.of() : headers) | ||
| .request(req); | ||
|
|
||
| if (body != null) { | ||
| builder.body(body.getBytes(StandardCharsets.UTF_8)); | ||
| } | ||
|
|
||
| return builder.build(); | ||
| } | ||
|
|
||
| @Test | ||
| void GET_500_is_retryable() { | ||
| Response res = newResponse(500, "GET", null, "{\"msg\":\"oops\"}"); | ||
| Exception ex = decoder.decode("key", res); | ||
| assertThat(ex).isInstanceOf(RetryableException.class); | ||
| } | ||
|
|
||
| @Test | ||
| void GET_429_with_retryAfter_is_retryable() { | ||
| Map<String, Collection<String>> headers = Map.of("Retry-After", List.of("2")); | ||
| Response res = newResponse(429, "GET", headers, null); | ||
| Exception ex = decoder.decode("key", res); | ||
| assertThat(ex).isInstanceOf(RetryableException.class); | ||
| } | ||
|
|
||
| @Test | ||
| void POST_500_is_NOT_retryable() { | ||
| Response res = newResponse(500, "POST", null, null); | ||
| Exception ex = decoder.decode("key", res); | ||
| assertThat(ex).isNotInstanceOf(RetryableException.class); | ||
| } | ||
|
|
||
| @Test | ||
| void GET_400_is_NOT_retryable() { | ||
| Response res = newResponse(400, "GET", null, null); | ||
| Exception ex = decoder.decode("key", res); | ||
| assertThat(ex).isNotInstanceOf(RetryableException.class); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Verification agent
🧩 Analysis chain
Double-check Feign maxAttempts semantics vs. the comment; adjust either comment or value
In feign.Retryer.Default(period, maxPeriod, maxAttempts), maxAttempts typically represents the total number of attempts (including the initial try), not the number of retries. Your comment says “최대 3번 재시도” which implies 3 retries (i.e., 4 total attempts), while the code sets 3 maxAttempts.
Option A (3 retries → 4 total attempts):
Option B (3 total attempts → fix comment):
To confirm semantics for your Feign version:
🌐 Web query:
💡 Result:
In OpenFeign's
Retryer.Defaultconstructor, the maxAttempts parameter represents the total number of attempts, including the initial call and all retries[3]. This means ifmaxAttemptsis set to 3, Feign will make up to three calls in total: the initial attempt plus up to two retries if necessary[3].Supporting details:
matez blogclarifies in a code example: by settingmaxAttemptsto 3, the test verifies that Feign makes a total of 3 attempts, not just 3 retries after the initial attempt[3].Key points:
Therefore, for Feign's
Retryer.Default, maxAttempts counts total attempts, including the first[3].Citations:
Fix Feign
Retryer.Defaultcomment to reflect total‐attempts semanticsFeign’s
maxAttemptsparameter counts total attempts (initial call + retries). WithmaxAttempts = 3, Feign performs up to 3 calls (1 initial + 2 retries). Update the comment in:• File:
src/main/java/com/example/spot/common/config/FeignConfig.javaLines: 19–23
If you actually want 3 retries (4 total attempts), change the code instead:
🤖 Prompt for AI Agents